• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Echoing out PHP array vars

Hey Experts!!

Very simple and straight forward question: In my WP site, I have gotten back an array and want to know how to echo out the values I need.

Let's say I want to echo out display_name from the following:

array(205) { [0]=> object(WP_User)#619 (7) { ["data"]=> object(stdClass)#393 (10) { ["ID"]=> string(4) "2138" ["user_login"]=> string(23) "blah@blah.com" ["user_pass"]=> string(34) "fdasfasfsfasfa" ["user_nicename"]=> string(22) "akillileawcapgroup-com" ["user_email"]=> string(23) "blah@blah.com" ["user_url"]=> string(13) "http://Alison" ["user_registered"]=> string(19) "2013-06-08 23:59:31" ["user_activation_key"]=> string(0) "" ["user_status"]=> string(1) "0" ["display_name"]=> string(6) "Alison" } ["ID"]=> int(2138) ["caps"]=>

Open in new window

How can I do that?
  • 2
1 Solution
echo $yourarray[0]->display_name;
To expand on my comment, you use [ ] brackets to access an array element by its index. Like:

$yourarray[0] is actually the WP_User object:
[0]=> object(WP_User)#619

Open in new window

To access a property of an object, you use the -> arrow:

Open in new window

So for example, you could change the object's display_name like this:
$object->display_name = "New Name";

Open in new window

or to display it:
echo $object->display_name;

Open in new window

My first example accesses the first array element, which is an object, and then accesses its display_name property.

Your array (whatever the variable name is) contains 205 elements:

Open in new window

So I'm assuming it is probably 205 unique WP_User objects. You could loop through the array and echo out all names like this:
foreach($yourarray as $user)
  echo $user->display_name . "<br>\n";

Open in new window

LZ1Author Commented:
Perfect thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now