php - Get key of multidimensional array? -
php - Get key of multidimensional array? -
for example, have multidimensional array below:
$array = array ( 0 => array ( 'id' => '9', 'gallery_id' => '2', 'picture' => '56475832.jpg' ), 1 => array ( 'id' => '8', 'gallery_id' => '2', 'picture' => '20083622.jpg' ), 2 => array ( 'id' => '7', 'gallery_id' => '2', 'picture' => '89001465.jpg' ), 3 => array ( 'id' => '6', 'gallery_id' => '2', 'picture' => '47360232.jpg' ), 4 => array ( 'id' => '5', 'gallery_id' => '2', 'picture' => '4876713.jpg' ), 5 => array ( 'id' => '4', 'gallery_id' => '2', 'picture' => '5447392.jpg' ), 6 => array ( 'id' => '3', 'gallery_id' => '2', 'picture' => '95117187.jpg' ) );
how can key of array(0,1,2,3,4,5,6)
?
i have tried lot of examples, nil has worked me.
this quite simple, need utilize array_keys()
:
$keys = array_keys($array);
see working
edit search task, function should job:
function array_search_inner ($array, $attr, $val, $strict = false) { // error input array not array if (!is_array($array)) homecoming false; // loop array foreach ($array $key => $inner) { // error if inner item not array (you may want remove line) if (!is_array($inner)) homecoming false; // skip entries search key not nowadays if (!isset($inner[$attr])) continue; if ($strict) { // strict typing if ($inner[$attr] === $val) homecoming $key; } else { // loose typing if ($inner[$attr] == $val) homecoming $key; } } // didn't find homecoming null; } // illustration usage $key = array_search_inner($array, 'id', 9);
the 4th parameter $strict
, if true
, utilize strict type comparisons. 9
not work, have pass '9'
, since values stored strings. returns key of first occurence of match, null
if value not found, or false
on error. create sure utilize strict comparing on homecoming value, since 0
, null
, false
possible homecoming values , evaluate 0
if using loose integer comparisons.
php arrays
Comments
Post a Comment