Solved

Echoing out PHP array vars

Posted on 2013-06-26
3
348 Views
Last Modified: 2013-06-26
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?
0
Comment
Question by:LZ1
  • 2
3 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39278138
echo $yourarray[0]->display_name;
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39278160
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:
$object->property

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:
array(205)

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

0
 
LVL 30

Author Closing Comment

by:LZ1
ID: 39278820
Perfect thanks!
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question