php - How to get key of an associative array by searching for offset value? -
using example , being aware of key,
$arr = array( 'product1'=>array('color'=>'blue','size'=>'medium'), 'product2'=>array('color'=>'green','size'=>'large'), 'product3'=>array('color'=>'yellow','size'=>'small'), );
is there method getting key in multidimensional array incremented value?
for example, i'd key of third array value in $arr
above. $arr[2]
return value (an array containing yellow/small).
is there way leverage key
function key numeric iterator, rather key "current position"?
or, there built-in function overlooking return key of $arr[2]
instead of it's value?
echo getkey($arr[2]); # returns product3
just use array_keys function :
$arr = array( 'product1'=>array('color'=>'blue','size'=>'medium'), 'product2'=>array('color'=>'green','size'=>'large'), 'product3'=>array('color'=>'yellow','size'=>'small'), ); $keys = array_keys($arr); echo $keys[2]; // shorter version echo array_keys($arr)[2];
more infos : http://php.net/manual/en/function.array-keys.php
Comments
Post a Comment