Hello all,
I am trying to find a way to loop through an associative array in order to return the index of each item in the array, for instance if I have the data(as returned from var_dump):
[text]
array
1 => string 'FIRST ITEM' (length=16)
112 => string 'SECOND ITEM' (length=19)
[/text]
I want to loop through the array and output the index of each item
so if I loop through the array my output should display 1, 112
I tried key($array) but this returns null. I am assuming this is looking for a string value
Thanks
return an index of an associative array
Moderator: General Moderators
Re: return an index of an associative array
Code: Select all
foreach($myArray as $index=>$value){
echo $index;
}Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: return an index of an associative array
You can also do:
that would output something like:
see http://php.net/array_keys for more info.
-Greg
Code: Select all
$aryKeys = array_keys($myArray);
echo implode(',',$aryKeys);Code: Select all
1,112-Greg
Re: return an index of an associative array
Thanks everyone, much appreciated