EDIT I have an associative array :
$formation_domains_info_array = array(
'electrotechnique-electronique' => array(
'title' => 'ÉLECTROTECHNIQUE - ÉLECTRONIQUE',
'flechage' => array(212, 213, 355),
),
'informatique-infographie-cao-dao' => array(
'title' => 'INFORMATIQUE - INFOGRAPHIE - CAO/DAO',
'flechage' => array(217, 218),
),
'metiers-du-verre-horlogerie' => array(
'title' => 'MÉTIERS DU VERRE - HORLOGERIE',
'flechage' => array(215, 224),
)
);
I wanted to iterate through it this way :
while ($index < count($fdomains_array)) {
echo $formation_domains_info_array[$index++] .'<br/>';
$index++;
}
I am quite surprised to get "Undefined offset 0
" as an error message. Is it not possible to iterate that way through associative array in PHP?
Thanks in advance for your explanations.
It is possible to iterate an associative array using while, but just not this way. You'll need to leverage the list
and each
methods as arguments to the while statement.
while(list($key, $value) = each($formation_domains_info_array)):
//do stuff here
endwhile;
But your best bet, would simply be a foreach loop.
foreach($formation_domains_info_array as $idx => $arr):
//do stuff here
endforeach;