Solved

Echoing out PHP array vars

Posted on 2013-06-26
3
341 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
Comment Utility
echo $yourarray[0]->display_name;
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
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
Comment Utility
Perfect thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…

772 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

14 Experts available now in Live!

Get 1:1 Help Now