Ageis Ageis - 1 month ago 6
PHP Question

Associative array: how to get the key of the current element which is an array

I have an array

Array (
[0] => Array ( [2006] => Array ( [0] => 12 ) )
[1] => Array ( [2004] => Array ( [0] => 12 [1] => 6 ) )
)


How do I get the year so I can generate the folllowing using unordered list

2006
December
2004
December
June

Answer

You can try:

$arr = Array(
  0 => Array(
    2006 => Array(
      0 => 12
    )
  ),
  1 => Array(
    2004 => Array(
      0 => 12,
      1 => 6
    )
  )
) ;

echo "<ul>\n";
foreach ($arr as $elem) {
  foreach ($elem as $year => $months) {
    echo "<li>$year\n<ul>\n";
    foreach ($months as $num => $month) {
        echo strftime("<li>%B</li>\n", mktime(0, 0, 0, $month));
    }
    echo "</ul>\n</li>\n";
  }
}
echo "</ul>\n";
Comments