【问题标题】:how to decrement a part in a value of an array?如何减少数组值中的一部分?
【发布时间】:2011-08-05 11:01:51
【问题描述】:

我有一个格式的数组

array() {["2011-07-29"]=> 39 ["2011-07-30"]=> 39 ["2011-07-31"]=> 39 ["2011-08-01"]=> 40}

我需要 t0 将中间键值减 1,即 ["2011-07-29"] 到 ["2011-06 em>-29"]

输出应该是

array() {["2011-06-29"]=> 39 ["2011-06-30"]=> 39 ["2011-06-31"]=> 39 ["2011-07-01"]=> 40}

如何做到这一点?

【问题讨论】:

    标签: php arrays multidimensional-array arraylist


    【解决方案1】:

    就像 Fernaref 解释的那样:通过解析键的值,根据需要更改键。有多种方法可以做到这一点,这只是一个例子 (Demo):

    <?php
    
    $data = array(
      '2011-07-29' => 39,
      '2011-07-30' => 39,
      '2011-07-31' => 39,
      '2011-08-01' => 40,
    );
    
    $keys = array_keys($data);
    
    foreach($keys as &$key)
    {
        list(,$month) = sscanf($key, '%d-%d-%d');
        $month = sprintf("%02d", $month-1);
        $key[5] = $month[0];
        $key[6] = $month[1];
    }
    unset($key);
    
    $data = array_combine($keys, $data);
    
    print_r($data);
    

    【讨论】:

    • 使用 substr 填充月份数的好技巧。我必须记住那个!
    • @Flambino:实际上我太懒了,也没有为它查找 sprintf 符号,这要好得多:$month = sprintf("%02d", $month-1); - 已编辑(但感谢您的反馈:))。
    • @hakre:我也太懒了,所以我在回答中使用了 str_pad :P——当然 sprintf 是正确的工具,但这不如重新利用 substr 有趣 :)
    • @Ezhil:请随意接受答案。 meta.stackexchange.com/questions/5234/…
    【解决方案2】:

    这是一个字符串 - 您必须解析出数据,减少值并将密钥重新组合在一起。或者首先使用更好的密钥。

    【讨论】:

      【解决方案3】:

      试试这个:

      $result = array();
      foreach ($array as $key => $val) {
        $date = strtotime ($key);
      
        $result[date("Y-m-d", strtotime("- month", $date)] = $val;
      }
      

      【讨论】:

      • -1,对我不起作用。 -1 month 或者您将数组减少为仅一个键,此外,如果该键不是进行减法的有效日期,它将被删除/覆盖。比较:codepad.org/4ACdinyA
      【解决方案4】:
      $input = array("2011-07-29"=>39, "2011-07-30"=>39, "2011-07-31"=>39, "2011-08-01"=>40);
      $output = array();
      
      foreach($input as $key => $value) {
          $key = preg_replace_callback("/(\d{4})-(\d{2})-/", function($match) {
              $match[2] = (int) $match[2] - 1;
              if( $match[2] < 1 ) { // don't forget to decrement the year, if the month goes below 1
                  $match[1] = (int) $match[1] - 1;
                  $match[2] = 12;
              }
              return $match[1] . "-" . str_pad($match[2], 2, "0", STR_PAD_LEFT) . "-";
          }, $key);
          $output[$key] = $value;
      }
      
      print_r($output);
      

      【讨论】:

        【解决方案5】:

        一些功能可以

        preg_replace("@(\d\d\d\d)-(\d\d)@e","'\$1-'.str_pad($2-1, 2, '0', STR_PAD_LEFT)",$key);
        

        【讨论】:

          【解决方案6】:
          $newArray = array();
          foreach ($array as $key => $value)
          {
            $newKey = someFunction($key);
            $newArray[$newKey] = $value;
          }
          

          虽然“someFunction”会将您的日期转换为创建新密钥

          【讨论】:

            猜你喜欢
            • 2018-10-18
            • 1970-01-01
            • 2020-10-31
            • 2018-02-16
            • 2020-04-12
            • 1970-01-01
            • 1970-01-01
            • 2018-04-24
            • 1970-01-01
            相关资源
            最近更新 更多