简体   繁体   中英

How can I create this multidimensional array from a foreach loop?

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.

Something like this...

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

This is how I would do it (assuming your array is called $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];
    }
}
$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];
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM