PHP - Get array value with a numeric index

thom picture thom · Jun 18, 2011 · Viewed 106.3k times · Source

I have an array like:

$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
echo native_function($array, 0); // bar
echo native_function($array, 1); // bin
echo native_function($array, 2); // ipsum

So, this native function would return a value based on a numeric index (second arg), ignoring assoc keys, looking for the real position in array.

Are there any native function to do that in PHP or should I write it? Thanks

Answer

genesis picture genesis · Jun 18, 2011
$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
$array = array_values($array);
echo $array[0]; //bar
echo $array[1]; //bin
echo $array[2]; //ipsum