Reputation: 3595
How do I reference the key of a multidimensional array? Here is the array:
Array
(
[Nov 18, 2011] => Array
(
[C] => 3
[I] => 1
)
[Nov 22, 2011] => Array
(
[C] => 2
)
)
and here is the foreach loop:
foreach ($array as $date) {
foreach ($date as $k => $v) {
// how to I reference the value of $billdate here ?
}
}
It is easy enough to reference the $k
and $v
inside the inner foreach
loop, but how do I reference the date value contained in the outer foreach
loop?
Upvotes: 2
Views: 15857
Reputation: 7055
You can get array keys by this way
print_r(array_keys(array_shift($array)));
Upvotes: 0
Reputation: 46987
Assuming $billdate
is the key of each top-level array:
foreach ($array as $billdate => $date) {
foreach ($date as $k => $v) {
var_dump($billdate, $k, $v);
}
}
Upvotes: 6
Reputation: 59699
Assign the key a value (apparently named $billdate
) in the outer foreach loop.
foreach( $array as $billdate => $date) {
foreach( $date as $k => $v) {
echo $billdate; // Prints something like Nov 18, 2011
}
}
Upvotes: 7