?
Solved

Reading an array

Posted on 2011-03-07
5
Medium Priority
?
230 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
[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
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 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