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@example.com
        [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@example.com
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
wow
  • 787
  • 1
  • 5
  • 5

6 Answers6

117

Look at the keys and indentation in your print_r:

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

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

...etc

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • 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? – Michel Joanisse Mar 26 '15 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 (http://php.net/manual/en/language.types.array.php#example-102). – Samuel Lindblom Apr 13 '15 at 13:54
45

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@example.com

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Virendra Yadav
  • 451
  • 4
  • 4
22

I think you want this:

foreach ($myarray as $key => $value) {
    echo "$key = $value\n";
}
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
Mike Axiak
  • 11,827
  • 2
  • 33
  • 49
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'));
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
1
echo $myarray[0]->['email'];

Try this only if it you are passing the stdclass object

Axel
  • 3,331
  • 11
  • 35
  • 58
  • 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 '17 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 :http://php.net/manual/en/language.types.object.php – Masad Ashraf Jul 31 '17 at 07:06
  • This definitely doesn't work. https://3v4l.org/VRo2O – mickmackusa Jun 28 '22 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@example.com'

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
v64
  • 320
  • 1
  • 4
  • 9