0

My foreach loop isn't displaying the values that match the keys they belong to .. I'm looping through this foreach loop to retrieve the names and images of my twitter followers and it's not displaying the values , I'm pretty sure the key names are correct .. Because when I just use the $value variable , it will echo both the name and image links are the same time but I want to catch the image link , put it in the img src html format to display the images .. Below is my code.

 for ($i=0; $i <= count($array); $i++) { if (is_array($array[$i])) { foreach($array[$i] as $key => $value) { if ($key =='name' || $key =='profile_image_url') { echo "<b>". $key["name"]."</b><br />"; echo "<img src='".$key['profile_image_url']."' width='100' height='100'/>"; } else { unset($key); } } } } 
2
  • Well, what is $value in the first place? Object? String? Use var_dump() to find out. Commented Dec 9, 2012 at 7:34
  • Please, format your code to help us read it Commented Dec 9, 2012 at 7:36

2 Answers 2

1

you have to print

$value 

and not

$key['name'] 

because as you state:

if($key =='name' || $key =='profile_image_url') 

then logically

 $key["name"] 

doesnt exist.

$key, in this case is either 'name' or 'profile_image_url'. It's a srting, not an array.

You need to do this:

for ($i=0; $i <= count($array); $i++) { if(is_array($array[$i])) { foreach($array[$i] as $key => $value) { if ($key =='name' || $key =='profile_image_url') { echo "<b>". $value."</b><br />"; //echo "<img src='".$key['profile_image_url']."' width='100' height='100'/>"; } else { unset($key); } } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

@no .. I want to display BOTH the Name and the PROFILE_IMAGE_URL in image format .. and they're all contained in $value .. i WANT TO display both the name and the image from the url ...
do this: print_r($array); so we can see what's in that array and how its structured
0

You use

$key["name"] 

to display an item. Shouldn't it be $value?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.