5

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

1
  • 4
    $slideitems is an array, not an object. Try echo $slideitems['image'] instead Commented May 28, 2013 at 12:37

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Just add caffeine and the error will disappear as if by magic :)
Haha true :) I'm blind without my coffe
2
 $newsitems[] = array( ... )

therefore you need

<?php echo $slideitems['image']

in your ourput loop.

Comments

1

$slideitems is an array not object So,

Change

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

to

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

Comments

1

In the first loop you assign array

$newsitems[] = array(

but here

$slideitems->image

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.