Brent
Brent

Reputation: 2485

PHP Foreach Empty

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

Upvotes: 5

Views: 271

Answers (5)

Brian Castellanos
Brian Castellanos

Reputation: 31

slideitems is not an object, it's an array, echo $slideitems["image"].

Upvotes: 1

user466764
user466764

Reputation: 1176

<?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.

Upvotes: 3

Voitcus
Voitcus

Reputation: 4446

In the first loop you assign array

$newsitems[] = array(

but here

$slideitems->image

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

Upvotes: 1

Prasanth Bendra
Prasanth Bendra

Reputation: 32760

$slideitems is an array not object So,

Change

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

to

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

Upvotes: 1

VolkerK
VolkerK

Reputation: 96159

 $newsitems[] = array( ... )

therefore you need

<?php echo $slideitems['image']

in your ourput loop.

Upvotes: 2

Related Questions