Solved

Reading an array

Posted on 2011-03-07
5
225 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

837 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