0

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.

9
  • Reason you need it in one line? Commented Apr 26, 2013 at 14:56
  • "The" key bar? Your array contains an "etc" part. Who says there's no other bar in there? Also, if you need to do it on one line, write a function. The function call would be an one-liner. Commented Apr 26, 2013 at 14:57
  • 1
    You are MASSSIVELY overthinking/overcomplicating things. Check out HankyPanky's answer below. Commented Apr 26, 2013 at 14:59
  • 1
    @Ash_ is it a multidimensional array ? I mean do you know the index 0 ? Commented Apr 26, 2013 at 15:03
  • 1
    @Ash_: Well, PHP 5.4 allows reset($array)['bar']. Is that what you want? Commented Apr 26, 2013 at 15:10

7 Answers 7

5

What stops you from using

$value=$myArray[0]["bar"];
echo $value;
Sign up to request clarification or add additional context in comments.

9 Comments

But that's too simple and doesn't call functions! :p
@jon: it is the answer, however. OP's obviously massively overcomplicating/overthinking things...
@Jon but isn't it what they are looking for?
@MarcB: How is it the answer? The OP clearly stated that they do not know the key 0. Of course there are many ways to work around that, but this answer doesn't show them. Also, I feel the fact that this answer is grammatically a question should be enough to skip the discussion. If you are not sure, use a comment to ask for more information first and then give an answer.
You become a bid childish, imho. I specified that I didn't know the index (in my example, 0). I'm sorry to irritating you.
|
2

Try this in PHP 5.4+ :

<?php
$a = array(array('foo' => 1111, 'bar' => 'example'));
print_r(current($a)['bar']);
?>

2 Comments

God, you're right : codepad.viper-7.com/fCiVWz Ok for PHP-5.4, that is great.
Yeah if there is no 'bar' key. But it is a notice - ok for me ! Both answer are good, so.
1

Don't ask me how LOL:

$a = array(array('foo' => 1111, 'bar' => 'example'));
echo array_map(function($v){if(isset($v['bar'])){return $v['bar'];}}, $a)[0];

Note:

  • This works only with the latest versions of PHP 5.4+

  • This will throw an error if there isn't a "bar" key

Online demo.


Better solution:

$a = array(
    array('aaa' => 2222, 'bbb' => '333'),
    array('foo' => 1111, 'bar' => 'example'),
    array('wut' => 'lol', 'zzz' => 'ggg')
);

echo array_values(array_filter(array_map(function($v){if(isset($v['bar'])){return $v['bar'];}}, $a)))[0];

Online demo.

1 Comment

Yeah it's pretty good. But well, you us a local function ;) EDIT : Look @b4v answer : codepad.viper-7.com/fCiVWz
0

do you need something like this?

$c = $b[0]['bar'];

Comments

0
$a = array(array('foo' => 1111, 'bar' => 'example'));
echo $a[0]['bar'];

example

Comments

0

If you don't want to use [0], you can use this instead:

$value = null;
foreach($array as $each)
{
   if(isset($each['bar']))
      $value = $each['bar'];
}

2 Comments

And I don't want a loop neither. ;)
Well, now you're just being difficult. :-P
0

Use this code:

echo $array[0]['bar'];

Or, using PHP functions:

echo array_pop(array_shift($array));

It will extract the first element of $array:

Array
(
    [foo] => 1111
    [bar] => example
)

Then extract and display the last element of this array:

example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.