Thomas Owens Thomas Owens - 1 month ago 6
PHP Question

Iterating over a complex Associative Array in PHP

Is there an easy way to iterate over an associative array of this structure in PHP:

The array

$searches
has a numbered index, with between 4 and 5 associative parts. So I not only need to iterate over
$searches[0]
through
$searches[n]
, but also
$searches[0]["part0"]
through
$searches[n]["partn"]
. The hard part is that different indexes have different numbers of parts (some might be missing one or two).

Thoughts on doing this in a way that's nice, neat, and understandable?

Answer

Nest two foreach loops:

foreach ($array as $i => $values) {
    print "$i {\n";
    foreach ($values as $key => $value) {
        print "    $key => $value\n";
    }
    print "}\n";
}
Comments