Solved

Echoing out PHP array vars

Posted on 2013-06-26
3
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Expert Comment

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The viewer will learn how to count occurrences of each item in an array.

749 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