Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this array

Array ( 
[0] => stdClass Object ( 
    [id] => 252062474) 
[1] => stdClass Object ( 
    [id] => 252062474) 
[3] => stdClass Object ( 
    [id] => 252062474) 
)

I need echo all of id's

I tried,

foreach($result as $item) { 
   echo $item->id;
}

but no luck

I try json_decode()

again no luck I use php 5.5.8

I know this work

echo $item[0]->id;

but i don't how many index is there

any idea?

share|improve this question
    
try json_decode($array,true), it will give u an array and u can loop through the array to get data. –  Abhik Chakraborty Feb 4 at 18:17
    
I tried but no luck recently I changed my php version to php 5.5.8 I think all problems goes there –  Yaşar Xavan Feb 4 at 19:08

4 Answers 4

Try this

foreach($result as $item) { 
   $item = (array)$item;
   echo $item['id'];
}
share|improve this answer

This array could be looped through and data could be retrieved. As you are saying its not working I have added the following code to illustrate how it works right from generating the array for you.

// generating an array like you gave in the example. Note that ur array has same value
// for all the ids in but my example its having different values
$arr = array();

$init = new stdClass;
$init->id = 252062474 ;
$arr[] = $init;
$init = new stdClass;
$init->id = 252062475 ;
$arr[] = $init;
$init = new stdClass;
$init->id = 252062476 ;
$arr[] = $init;
print_r($arr);

The above array is same as yours

Array ( [0] => stdClass Object ( [id] => 252062474 )
        [1] => stdClass Object ( [id] => 252062475 ) 
        [2] => stdClass Object ( [id] => 252062476 )
      )

Now the following code will loop through and get the data as

foreach($arr as $key=>$val){
    echo $key.'  ID is :: '.$val->id;
    echo '<br />';
}

The output will be

0 ID is :: 252062474
1 ID is :: 252062475
2 ID is :: 252062476
share|improve this answer

Try this-

Code

foreach($result as $p) {
    echo $p['id'] . "<br/>";
}

Output

252062474
252062474
252062474
share|improve this answer
    
returns: 00 var_dump($p['id']) returns: NULL NULL string(1) "0" NULL string(1) "0" –  Yaşar Xavan Feb 4 at 19:01

Maybe you are confused on foreach(). If this works:

echo $item[0]->id;

Then you would need:

foreach($item as $result) {
    echo $result->id;
}
share|improve this answer
    
var_dump($result->id) shows NULL –  Yaşar Xavan Feb 4 at 18:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.