76

Example print_r($myarray)

Array
(
    [0] => Array
    (
        [id] => 6578765
        [name] => John Smith
        [first_name] => John
        [last_name] => Smith
        [link] => http://www.example.com
        [gender] => male
        [email] => [email protected]
        [timezone] => 8
        [updated_time] => 2010-12-07T21:02:21+0000
    )
)

Question, how to get the $myarray in single value like:

echo $myarray['email'];  will show [email protected]
4
  • 4
    Can you clarify what you mean by "single format"?
    – Arkaaito
    Dec 8, 2010 at 3:33
  • I don't know how to call it. but from the array i want to get the data one by one.
    – wow
    Dec 8, 2010 at 3:36
  • 2
    echo $myarray['email'] DOES show [email protected], a single value. It is not clear what you're asking! Dec 8, 2010 at 3:38
  • @Dan Grossman LOL Okay let's me make example. $myarray from this array how do I get the output for first_name, gender or etc.
    – wow
    Dec 8, 2010 at 3:48

6 Answers 6

117

Look at the keys and indentation in your print_r:

echo $myarray[0]['email'];

echo $myarray[0]['gender'];

...etc

2
  • I can echo a key in the following way but only on my local instance: echo myarray()["baseurl"]; when I push to a remote server, I get a parse / syntax error. Why is that? Mar 26, 2015 at 14:12
  • 2
    @MichelJoanisse probably because you are using an older PHP version on your remote server. That syntax is not supported in PHP <5.4 (php.net/manual/en/language.types.array.php#example-102). Apr 13, 2015 at 13:54
46

Use array_shift function

$myarray = array_shift($myarray);

This will move array elements one level up and you can access any array element without using [0] key

echo $myarray['email'];

will show [email protected]

22

I think you want this:

foreach ($myarray as $key => $value) {
    echo "$key = $value\n";
}
2
  • yes! this one. I got return 0 = Array but when I print_r($myarray) showing the output.
    – wow
    Dec 8, 2010 at 3:43
  • Got it by put $myarray[0]. Thanks make me learnt a new thing :P
    – wow
    Dec 8, 2010 at 4:03
13

You can also use array_column(). It's available from PHP 5.5: php.net/manual/en/function.array-column.php

It returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.

print_r(array_column($myarray, 'email'));
0
1
echo $myarray[0]->['email'];

Try this only if it you are passing the stdclass object

3
  • 1
    Could you give more context to the original poster? Like explain what is not working in his code and how the suggested change will fix it. And maybe detail what is stdclass object.
    – JeromeFr
    Feb 22, 2017 at 12:12
  • 1
    he is getting 2d array , and wants a first value of 2d array . So if he is passing his object from controller to view , like in Laravel (MVC framework), his views get stdclass object. And from that stdclass object , if a person wants single value , he can print his value as i mentioned above . For more stdclass objects in php link :php.net/manual/en/language.types.object.php Jul 31, 2017 at 7:06
  • This definitely doesn't work. 3v4l.org/VRo2O Jun 28, 2022 at 10:43
1

The first element of $myarray is the array of values you want. So, right now,

echo $myarray[0]['email']; // This outputs '[email protected]'

If you want that array to become $myarray, then you just have to do

$myarray = $myarray[0];

Now, $myarray['email'] etc. will output as expected.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.