2

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.

2
  • 1
    If you don't need the date value, I suggest you remove it first. Also what have you done so far? Post your code please otherwise do your homework first. Commented Apr 13, 2012 at 19:13
  • Sorry, I have added what code that I have...I can also change the index to get the state or city array, but just not sure how to piece it altogether. Commented Apr 13, 2012 at 19:18

5 Answers 5

1

Something like this...

$result=array();
foreach ($a as $item)
    $result[$item[1]][$item[2]][$item[3]]+=$item[4]; 
Sign up to request clarification or add additional context in comments.

7 Comments

This is probably solvable, pretty easily, but ++ won't work, as it doesn't just need an incrementor, it needs to be incremented by the value in the array.
Instead of = 0, it should be = $item[4], and instead of ++ it should be += $item[4]. The result should not be initialized to 0 because it begins with an initial value.
Yes, these changes have been made.
Im not really sure what you want. it would have worked in just about any form I had on here. Vote, don't vote, thats fine :)
Yes, there are better ways of doing everything. This is a demonstration, and will yield the correct result. Some warnings are more serious that others.
|
1

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

Comments

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 Comments

Should be initialized to $data[4], not 0.
Not if he is adding $data[4] on the next line, regardless.
@sberry That is true. I take it back. :)
too much work. PHP handles array access on the fly, you don't need to check for each index, you can isset check for the whole index at once.
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)
    }
  }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.