Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Can't get values from this array.
#1

[eluser]chefnelone[/eluser]
Hello

I'm having somme problem when I try to get a value from an array.

If I run these lines in my view:
Code:
<?php
print_r($images); <!-- JUST TO CHECK THAT THE ARRAY IS THERE -->
?>


<?php
foreach($images as $image){
echo $image->image_name;  <!-- THIS IS LINE 26 -->
}
?>

I get this in the browser:

Code:
Array ( [images] => Array ( [0] => stdClass Object ( [id_product_image] => 1 [id_product] => 13 [image_name] => mesa-servida.jpg [product_image_title] => [product_image_descripription] => [orden] => 0 ) ) )

____________________________________________________________________
      A PHP Error was encountered

      Severity: Notice

      Message: Trying to get property of non-object

      Filename: includes/editor_producto_fotos.php

      Line Number: 26
____________________________________________________________________
I can see tha the array is there but it fail when trying to get the value using: $image->image_name;
How do I get the values from the array?
#2

[eluser]Rob Gordijn[/eluser]
try:

Code:
foreach($images->images as $image){
echo $image->image_name;
}

seems like $images holds an array [images] with 1 entry, (your image object) that objects holds image_name
#3

[eluser]chefnelone[/eluser]
[quote author="Rob Gordijn" date="1265220350"]try:

Code:
foreach($images->images as $image){
echo $image->image_name;
}

seems like $images holds an array [images] with 1 entry, (your image object) that objects holds image_name[/quote]

tried, still not working, it seems that it doesn't accept the argument for foreach()

Code:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: includes/editor_producto_fotos.php

Line Number: 25

Line Number: 25 is:
Code:
foreach($images->images as $image){
#4

[eluser]Rob Gordijn[/eluser]
lol, my bad.

a little re-production:

Code:
class test{
    var $bla = 'foo';
}

$images = array(
    'images' => array(
        new test(),
    )
);
print_r($images);
echo '<br>';
foreach($images['images'] as $image){
    echo $image->bla;
}

not $images->images (object call) but $images['images'] (array call)

cheers Tongue
#5

[eluser]chefnelone[/eluser]
[quote author="Rob Gordijn" date="1265221296"]lol, my bad.

a little re-production:

Code:
class test{
    var $bla = 'foo';
}

$images = array(
    'images' => array(
        new test(),
    )
);
print_r($images);
echo '<br>';
foreach($images['images'] as $image){
    echo $image->bla;
}

not $images->images (object call) but $images['images'] (array call)

cheers Tongue[/quote]
works!
:-)
#6

[eluser]Rob Gordijn[/eluser]
yeah! Woohoo!




Theme © iAndrew 2016 - Forum software by © MyBB