views:

42

answers:

3

I have a multidimensional array, and I would have multiple arrays within it. Some of those arrays contain multiple arrays within them as well, and I would like to count how many arrays are within the second array(the date).

This is an example of the structure of the multidimensional array:

$_SESSION['final_shipping'][04/03/2010][book]
$_SESSION['final_shipping'][04/12/2010][magazine]
$_SESSION['final_shipping'][04/12/2010][cd]

This is the foreach statement I am currently using to count how many of the second array(the one with the dates) exists.

foreach($_SESSION['final_shipping'] as $date_key => $date_value) { 
    foreach ($date_value as $product_key => $product_value) { 
        echo 'There are ' . count($date_key) . ' of the ' . $date_key . ' selection.<br/>';
    }
}

It is currently outputting this:

There are 1 of the 04/03/2010 selection.
There are 1 of the 04/12/2010 selection.
There are 1 of the 04/12/2010 selection.

I would like it to output this:

There are 1 of the 04/03/2010 selection.
There are 2 of the 04/12/2010 selection.

+4  A: 

Call count() on $date_value instead, since you want to count the number of items in the array value mapped to that key, not the size of the key itself.

foreach($_SESSION['final_shipping'] as $date_key => $date_value) {
    echo 'There are ' . count($date_value) . ' of the ' . $date_key . ' selection.<br/>';
}
Amber
I was so close. Thanks alot!
zeckdude
+2  A: 

You are counting the wrong varibale it needs to be $date_value

foreach($_SESSION['final_shipping'] as $date_key => $date_value) { 
    echo 'There are ' . count($date_value) . ' of the ' . $date_key . ' selection.<br/>';
}
Lizard
A: 

With a multidimensional array like this:

$data = array(
    'item1' => array(
        'item1-1' => array( 'foo' => 1,'bar' => 1 ),
        'item1-2' => array( 'bar' => 1 ),
    ),
    'item2' => array( 
        'item2-1' => array('foo' => 1 )
    )
);

You can create a RecursiveIteratorIterator and RecursiveArrayIterator

$it = new RecursiveIteratorIterator(
        new RecursiveArrayIterator($data),
        RecursiveIteratorIterator::SELF_FIRST);

and then iterate over it easily with foreach

foreach($it as $key => $val) {
    if(is_array($val)) {
        printf('There is %d of %s%s', count($val), $key, PHP_EOL);
    }
}

to receive

There is 2 of item1
There is 2 of item1-1
There is 1 of item1-2
There is 1 of item2
There is 1 of item2-1

You can limit this to only return the arrays of a certain depth with

foreach($it as $key => $val) {
    if(is_array($val) && $it->getDepth() === 1) {
        printf('There is %d of %s%s', count($val), $key, PHP_EOL);
    }
}

to receive

There is 2 of item1-1
There is 1 of item1-2
There is 1 of item2-1
Gordon
It's good to see this sort of approach coming across in answers but the example given is not particularly applicable to the question.
salathe
@salathe what do you think is missing?
Gordon
I guess the main thing is satisfying the request to "count how many arrays are within the second array(the date)" rather than a generic count-all-levels solution.
salathe
@salathe I added that. But I agree the above is a bit overkill in the OPs case, since he is not really counting on a multidimensional level, but just the arrays on a single level.
Gordon