문제

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.

도움이 되었습니까?

해결책

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

if (isset($total[0])) echo $total[0]->log_pk

다른 팁

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;

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

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])

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top