How do I count how many arrays have the same name within a multidimensional array with php?
Posted
by zeckdude
on Stack Overflow
See other posts from Stack Overflow
or by zeckdude
Published on 2010-04-07T09:36:43Z
Indexed on
2010/04/07
9:43 UTC
Read the original article
Hit count: 244
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.
© Stack Overflow or respective owner