【问题标题】:How can I create this multidimensional array from a foreach loop?如何从 foreach 循环创建这个多维数组?
【发布时间】:2012-04-26 04:12:32
【问题描述】:

我无法解决这个问题...我有一个看起来像这样的数组:

    Array
(
    [0] => Array
        (
            [0] => 20120412
            [1] => United States
            [2] => Illinois
            [3] => Marion
            [4] => 2
        )

    [1] => Array
        (
            [0] => 20120412
            [1] => United States
            [2] => Illinois
            [3] => Carbondale
            [4] => 2
        )

    [2] => Array
        (
            [0] => 20120412
            [1] => United States
            [2] => Illinois
            [3] => Carbondale
            [4] => 2
        )
)

我希望它是这样的:

array("United States" => array("Illinois" => array("Carbondale" => "4")));

这样它会先取出国家,然后取出州,然后将城市的所有数字相加。

到目前为止,我只有:

foreach($location_google_data3 as $location_google_data4){
    if($location_google_data4[0]==date("Ymd")){
         $today_visitsbycountry[$location_google_data4[1]]+=$location_google_data4[4];  
    }
}

这给了我一个包含国家和访问次数的数组,以便我以后可以遍历它,但不知道如何继续其余的。

【问题讨论】:

  • 如果不需要日期值,建议先去掉。还有你到目前为止做了什么?请发布您的代码,否则请先做好功课。
  • 抱歉,我已经添加了我拥有的代码...我还可以更改索引以获取州或城市数组,但不知道如何将它完全拼凑起来。

标签: php multidimensional-array foreach


【解决方案1】:

这样的……

$result=array();
foreach ($a as $item)
    $result[$item[1]][$item[2]][$item[3]]+=$item[4]; 

【讨论】:

  • 这可能很容易解决,但++ 不起作用,因为它不仅需要增量器,还需要按数组中的值递增。
  • 应该是= $item[4],而不是= 0,而不是++,它应该是+= $item[4]。结果不应初始化为 0,因为它以初始值开头。
  • 是的,这些更改已经完成。
  • 我不太确定你想要什么。它几乎可以以我在这里使用的任何形式工作。投票,不投票,很好:)
  • 是的,有更好的方法来做每一件事。这是一个演示,将产生正确的结果。有些警告比其他警告更严重。
【解决方案2】:

我会这样做(假设您的数组名为$input):

$output = array();

foreach ($input as $city) {
    if (!isset($output[$city[1]])) {
        $output[$city[1]] = array();
    }

    if (!isset($output[$city[1]][$city[2]])) {
        $output[$city[1]][$city[2]] = array();
    }

    if (isset($output[$city[1]][$city[2]][$city[3]])) {
        $output[$city[1]][$city[2]][$city[3]] += $city[4];
    } else {
        $output[$city[1]][$city[2]][$city[3]] = $city[4];
    }
}

【讨论】:

    【解决方案3】:

    我希望这会奏效,如果没有的话 - 像这样的东西可以解决问题:

    $result = array();
    foreach( $your_array as $arr ) {
        if( !isset( $result[ $arr[1] ] ) ) {
            $result[ $arr[1] ] = array();
        }
        if( !isset( $result[ $arr[1] ][ $arr[2] ] ) ) {
            $result[ $arr[1] ][ $arr[2] ] = array();
        }
        if( !isset( $result[ $arr[1] ][ $arr[2] ][ $arr[3] ] ) ) {
            $result[ $arr[1] ][ $arr[2] ][ $arr[3] ] = 0;
        }
        $result[ $arr[1] ][ $arr[2] ][ $arr[3] ] += (int)$arr[4];
    }
    

    【讨论】:

      【解决方案4】:
      $resultArray = array();
      foreach($yourArray as $data)
      {
          if(!isset($resultArray[$data[1]])) {
              $resultArray[$data[1]] = array();
          }
      
          if(!isset($resultArray[$data[1]][$data[2]])) {
              $resultArray[$data[1]][$data[2]] = array();
          }
      
          if(!isset($resultArray[$data[1]][$data[2]][$data[3]])) {
              $resultArray[$data[1]][$data[2]][$data[3]] = 0;
          }
      
          $resultArray[$data[1]][$data[2]][$data[3]] += $data[4];
      }
      

      【讨论】:

      • 应该初始化为$data[4],而不是0。
      • 无论如何,如果他在下一行添加$data[4],则不会。
      • @sberry 确实如此。我把它收回。 :)
      • 工作量太大。 PHP 动态处理数组访问,您不需要检查每个索引,您可以isset 一次检查整个索引。
      【解决方案5】:

      您可以通过从数组中选取值,将它们用作新数组的键,然后添加数字来执行此操作。这个例子使用了变量别名(引用),所以长版本的变量只需要写一次:

      $filterDate = '20120412';
      $build = array();
      foreach ($array as $item)
      {
          list($date, $country, $state, $city, $number) = $item;
          if ($date != $filterDate) continue;
          $alias = &$build[$country][$state][$city];
          $alias += $number;
      }
      unset($alias);
      

      结果($build):

      array(1) {
        ["United States"]=>
        array(1) {
          ["Illinois"]=>
          array(2) {
            ["Marion"]=>
            int(2)
            ["Carbondale"]=>
            int(4)
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-29
        • 1970-01-01
        • 2018-06-12
        • 1970-01-01
        • 2015-06-02
        • 2014-06-26
        • 1970-01-01
        相关资源
        最近更新 更多