如何从foreach循环创建这个多维数组?

时间:2021-06-24 22:28:59

I cannot wrap my head around this...I have an array that looks like:

我无法绕过这个...我有一个看起来像这样的数组:

    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
        )
)

I am wanting it to be like:

我希望它像:

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

So that it takes the Country out, Then the State, then adds together all of the city's numbers.

因此,它需要国家,然后国家,然后将所有城市的数字加在一起。

So far all I have is:

到目前为止我只有:

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];  
    }
}

This gives me an array with the country and number of visits so that I can iterate through it later, but not sure how to proceed with the rest.

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

5 个解决方案

#1


1  

Something like this...

这样的东西......

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

#2


1  

This is how I would do it (assuming your array is called $input):

我就是这样做的(假设你的数组被称为$ 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


1  

$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];
}

#4


1  

You do this by picking the values from the array, use them as keys for the new array and then add the number. This example uses a variable alias (reference), so the long version of the variable needs only written once:

您可以通过从数组中选择值,将它们用作新数组的键,然后添加数字来完成此操作。此示例使用变量别名(引用),因此变量的长版本只需编写一次:

$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);

Outcome ($build):

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

#5


0  

I hope this will work, if not - something like this will do the trick:

我希望这会起作用,如果没有 - 这样的事情会起作用:

$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];
}

#1


1  

Something like this...

这样的东西......

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

#2


1  

This is how I would do it (assuming your array is called $input):

我就是这样做的(假设你的数组被称为$ 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


1  

$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];
}

#4


1  

You do this by picking the values from the array, use them as keys for the new array and then add the number. This example uses a variable alias (reference), so the long version of the variable needs only written once:

您可以通过从数组中选择值,将它们用作新数组的键,然后添加数字来完成此操作。此示例使用变量别名(引用),因此变量的长版本只需编写一次:

$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);

Outcome ($build):

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

#5


0  

I hope this will work, if not - something like this will do the trick:

我希望这会起作用,如果没有 - 这样的事情会起作用:

$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];
}