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"? Commented 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. Commented 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! Commented 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. Commented Dec 8, 2010 at 3:48

6 Answers 6

116

Look at the keys and indentation in your print_r:

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

...etc

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@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).
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]

Comments

22

I think you want this:

foreach ($myarray as $key => $value) { echo "$key = $value\n"; } 

2 Comments

yes! this one. I got return 0 = Array but when I print_r($myarray) showing the output.
Got it by put $myarray[0]. Thanks make me learnt a new thing :P
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')); 

Comments

1
echo $myarray[0]->['email']; 

Try this only if it you are passing the stdclass object

3 Comments

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.
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
This definitely doesn't work. 3v4l.org/VRo2O
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.