Solved

Reading an array

Posted on 2011-03-07
5
224 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
ID: 35057830
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
ID: 35058036
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
ID: 35058188
>>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
ID: 35058385
Yeah..that worked thanks!
0
 
LVL 34

Expert Comment

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

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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