Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Foreach Empty

Tags:

foreach

php

I cant seem to echo the values inside my foreach array, my code so far.

<?php
foreach ($results as $item) {

    $imgData = json_decode($item->params, true);
    // create array
    $newsitems[] = array(
        'name' => $item->name,
        'url'  => $item->clickurl,
        'custom'  => $item->custombannercode,
        'image' => $imgData['imageurl']
    );              
}
?>

<?php foreach ($newsitems as $slideitems) {  ?>
  <li> 
     <img src="<?php echo $slideitems->image; ?>" > 
  </li>
<?php }; ?>

I get two list items which is correct but when i try to echo out any values it shows blank, am I doing this correct?

Thanks

like image 225
Brent Avatar asked May 28 '13 12:05

Brent


4 Answers

<?php foreach ($newsitems as $slideitems) {  
  var_dump($slideitems); ?>
  <li> 
     <img src="<?php echo $slideitems['image']; ?>" > 
  </li>
<?php }; ?>

You could try a var_dump to see what values you're getting. Also as slideitems is an array check the line that outputs the img src.

I hope this helps.

like image 129
user466764 Avatar answered Sep 21 '22 06:09

user466764


 $newsitems[] = array( ... )

therefore you need

<?php echo $slideitems['image']

in your ourput loop.

like image 22
VolkerK Avatar answered Sep 19 '22 06:09

VolkerK


$slideitems is an array not object So,

Change

<?php echo $slideitems->image; ?>

to

<?php echo $slideitems['image']; ?>

like image 34
Prasanth Bendra Avatar answered Sep 20 '22 06:09

Prasanth Bendra


In the first loop you assign array

$newsitems[] = array(

but here

$slideitems->image

you're referencing to object. consider using $slideitems['image']

like image 26
Voitcus Avatar answered Sep 23 '22 06:09

Voitcus