Solved

Reading an array

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

726 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