Solved

Reading an array

Posted on 2011-03-07
5
223 Views
Last Modified: 2012-05-11
Hi all,

I am doing a var_dump on a variable and it's returning the following:

var_dump($theArrayVar);

THis returns:

object(stdClass)#2 (13) { ["id"]=> string(15) "1233434333" ["name"]=> string(21) "the name" ["first_name"]=> string(6) "the" ["last_name"]=> string(14) "name name" ["link"]=> string(54) "http://www.mysite.com/profile.php?id=1233434333" ["hometown"]=> object(stdClass)#3 (2) { ["id"]=> string(15) "1233434333" ["name"]=> string(23) "Los Angeles, California" } ["location"]=> object(stdClass)#4 (2) { ["id"]=> string(15) "1233434333" ["name"]=> string(16) "Honolulu, Hawaii" } ["bio"]=> string(12) "I like Music" ["gender"]=> string(4) "male" ["locale"]=> string(5) "en_GB" ["languages"]=> array(1) { [0]=> object(stdClass)#5 (2) { ["id"]=> string(15) "1233434333" ["name"]=> string(7) "English" } } ["verified"]=> bool(true) ["updated_time"]=> string(24) "2011-03-07T15:12:51+0000" }


I am trying to read it this way:

echo  theArrayVar[0];
echo  theArrayVar[1];
etc...

but it's not working ... anyone going any ideas of why/what I'm doing wrong please?

Thanks
0
Comment
Question by:error77
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
Two things. First

echo  theArrayVar[0];

will never work because the variable name needs a $

echo  $theArrayVar[0];


Secondly - look at the dump and you will see that the elements have names like 'id' or 'name' and not 0, 1, 2, etc. This leaves you with two options:

1: Use foreach to read the array

foreach( $theArrayVar as $key => value )
    echo "\$theArrayVar['$key'] has value $value<br/>";


2. Use array_values to build a new array numbered from 0 upwards

   $newArray = array_values( $theArrayVar );
   echo $newArray[0];
   echo $newArray[1];


I advise you to master foreach because it is so very, very useful.

http://www.php.net/foreach
http://www.php.net/array_values
0
 

Author Comment

by:error77
Comment Utility
Getting nothing reuturned for some reason. Syntax? something in the foreach loop isn't working.

Some more info:

This is the line that gets the data:

$theArrayVar = json_decode(file_get_contents('https://graph.webgsite.com/me?access_token=' .$session['access_token']));

any ideas please?

thanks again
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
>>I am trying to read it this way:
>>echo  $theArrayVar[0];
>>echo  $theArrayVar[1];

 $theArrayVar is NOT an array. It is an object. So if you want to read the "name" you need to use 'arrow' notation:
echo $theArrayVar->name;

The same goes for the other properties:
echo $theArrayVar->id;

The "languages" is:
echo $theArrayVar->languages[0]->name;

It will help you "see" the right syntax if you format/indent the dump you are getting:

object(stdClass)#2 (13) 
{ 
	["id"]=> string(15) "1233434333" 
	["name"]=> string(21) "the name" 
	["first_name"]=> string(6) "the" 
	["last_name"]=> string(14) "name name" 
	["link"]=> string(54) "http://www.mysite.com/profile.php?id=1233434333" 
	["hometown"]=> object(stdClass)#3 (2) 
				{ 
					["id"]=> string(15) "1233434333" 
					["name"]=> string(23) "Los Angeles, California" 
				} 
	["location"]=> object(stdClass)#4 (2) 
				{ 
					["id"]=> string(15) "1233434333" 
					["name"]=> string(16) "Honolulu, Hawaii" 
				} 
	["bio"]=> string(12) "I like Music" 
	["gender"]=> string(4) "male" 
	["locale"]=> string(5) "en_GB" 
	["languages"]=> array(1) 
				{ 
					[0]=> object(stdClass)#5 (2) 
						{ 
							["id"]=> string(15) "1233434333" 
							["name"]=> string(7) "English" 
						} 
				} 
	["verified"]=> bool(true) 
	["updated_time"]=> string(24) "2011-03-07T15:12:51+0000" 
}

Open in new window

0
 

Author Closing Comment

by:error77
Comment Utility
Yeah..that worked thanks!
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
Well spotted Hielo.... I just took his word it was an array....
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

11 Experts available now in Live!

Get 1:1 Help Now