I have this array - it is a multidimensional array, and i don't know the 0 index :
Array
(
[0] => Array
(
[foo] => 1111
[bar] => example
)
)
And I need, in one line, with native php functions, to get the value from the key bar. I tried much combination with current, key, array_keys, and so on.
With current($array) I got :
Array
(
[foo] => 1111
[bar] => example
)
I discover array_keys, which allow me, with the second argument, to specify a value to find. But I didn't know the value, only the key, and array_values didnt propose a second argument to find (:o).
Here I am now : current(array_keys(array_flip(current($b))), 'bar') didn't work. But
$c = array_flip(current($b));
print_r(current(array_keys($c, 'bar')));
works - I get "example". (By the way, why these 2 lines are working and not the previous one ?).
And I know, it's ugly to use array_flip for this, but I don't know how to do otherwise. Any idea ? Thanks.
bar? Your array contains an "etc" part. Who says there's no otherbarin there? Also, if you need to do it on one line, write a function. The function call would be an one-liner.reset($array)['bar']. Is that what you want?