Solved

Echoing out PHP array vars

Posted on 2013-06-26
3
344 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Inserting meta tags into the <head> . . .</head> area created by get_header() 3 32
mcrypt_create_iv() is deprecated 4 74
Update from TABLE-A to TABLE-B 5 39
PHP warning 4 30
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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now