0

I did a print_r on my array $total and it returned the following:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => stdClass Object ( 
[generated] => 6 [magnitude] => 3 [log_pk] => 14 [result] => 0.5000 ) )
Array ( ) Array ( )

I need to be able to print out log_pk from within the stdClass Object. I have tried print $total[0]->log_pk but that was unsuccessful. The error was Undefined offset: 0. Any help is appreciated. Thanks.

4

4 回答 4

1

Are you doing this within a loop? If so then it looks like the array is empty on most iterations. Try:

if (!empty($total)) print $total[0]->log_pk;

于 2013-08-10T02:39:28.840 回答
1

So this is within a loop, you should check if the index 0 exists first.

if (isset($total[0])) echo $total[0]->log_pk
于 2013-08-10T02:46:15.497 回答
0

var_dump() displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

var_dump($total)

PHP: var_dump - Manual

于 2013-08-10T02:39:00.913 回答
0

it looks like your print_r is inside a loop.

while(true){
    $total = some_function();
    print_r($total);
    if($condition) break;
}
// Here - outside the loop, is not the right place for the print_r();

If you want you print outside the loop, you would change $total = some_function(); to $total[] = some_function(); and then you can do print_r($total[index])

于 2013-08-10T02:43:22.553 回答