简体   繁体   English

如何从数组数组中的stdClass对象访问变量?

[英]How do I access a variable from an stdClass object within an Array of Arrays?

I did a print_r on my array $total and it returned the following: 我在我的数组$total上做了一个print_r ,它返回了以下内容:

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 . 我需要能够从stdClass Object打印出log_pk I have tried print $total[0]->log_pk but that was unsuccessful. 我试过print $total[0]->log_pk但是没有成功。 The error was Undefined offset: 0 . 错误是Undefined offset: 0 Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

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;

所以这是一个循环,你应该先检查索引0是否存在。

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

var_dump() displays structured information about one or more expressions that includes its type and value. var_dump()显示有关包含其类型和值的一个或多个表达式的结构化信息。 Arrays and objects are explored recursively with values indented to show structure. 递归地探索数组和对象,其中值缩进以显示结构。

var_dump($total)

PHP: var_dump - Manual PHP:var_dump - 手动

it looks like your print_r is inside a loop. 看起来你的print_r在循环中。

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(); 如果你想在循环外打印,你会改变$total = some_function(); to $total[] = some_function(); to $total[] = some_function(); and then you can do print_r($total[index]) 然后你可以做print_r($total[index])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM