简体   繁体   English

致命错误:未被捕获的错误:不能将Entrada类型的对象用作数组

[英]Fatal error: Uncaught Error: Cannot use object of type Entrada as array

Who help me with this problem? 谁来帮助我解决这个问题?

56    public static function mostrar_entradas_busqueda($entradas) {
57            $count = count($entradas);
58            for ($i = 1; $i <= $count; $i++) {
59                if ($i % 3 == 1) {
60                    ?>
61                    <div class="row"> 
62                        <?php
63                    }
64                    $entradas = $entradas[$i - 1];
65                    self::mostrar_entrada_busqueda($entradas);
66                    
67                    if ($i % 3 == 0) {
68                        ?>
69                    </div>
70                        <?php
71                    }
72                }
73                if ($i % 3 !== 0) {
74                    ?>
75                    </div>
76                    <?php
77                }
78            }

Fatal error: Uncaught Error: Cannot use object of type Entrada as array in C:\\xampp\\htdocs\\blog\\app\\EscritorEntradas.inc.php:64 Stack trace: #0 C:\\xampp\\htdocs\\blog\\vistas\\buscar.php(81): EscritorEntradas::mostrar_entradas_busqueda(Object(Entrada)) #1 C:\\xampp\\htdocs\\blog\\index.php(117): include_once('C:\\xampp\\htdocs...') #2 {main} thrown in C:\\xampp\\htdocs\\blog\\app\\EscritorEntradas.inc.php on line 64 致命错误:未捕获错误:无法在C:\\ xampp \\ htdocs \\ blog \\ app \\ EscritorEntradas.inc.php:64中将Entrada类型的对象用作数组:#0 C:\\ xampp \\ htdocs \\ blog \\ vistas \\ buscar .php(81):EscritorEntradas :: mostrar_entradas_busqueda(Object(Entrada))#1 C:\\ xampp \\ htdocs \\ blog \\ index.php(117):include_once('C:\\ xampp \\ htdocs ...')#2 {main}在第64行的C:\\ xampp \\ htdocs \\ blog \\ app \\ EscritorEntradas.inc.php中引发

The first time you execute it, this line... 第一次执行时,此行...

$entradas = $entradas[$i - 1];

... rewrites the value of $entradas to an object (value of the very zeroth element of $entradas array). ...将$entradas的值重写为一个对象( $entradas数组的第零个元素的值)。 The next time you try to execute it, you essentially query that object as an array, hence the error. 下次尝试执行该命令时,实际上是将该对象作为数组查询,因此会出现错误。

Solution: just rename that variable, and you're done: 解决方案:只需重命名该变量,即可完成:

$entrada = $entradas[$i - 1]; // and now it's single
self::mostrar_entrada_busqueda($entrada);

And yes, variables are scoped per function in PHP, not per block. 是的,变量是PHP中每个函数的作用域,而不是每个块。 But even if the latter were the case, you'd still have to rewrite that line; 但是,即使是后者,您仍然必须重写该行。 I'm not aware of any language that has different rules of scope resolution for right and left parts of assignment statements. 我不知道任何语言对赋值语句的左右部分具有不同的范围解析规则。

As a sidenote, I'm really not sure why you check for i % 3 == 0 inside if (i % 3 == 1) branch. 附带说明,我真的不确定为什么要在if (i % 3 == 1)分支中检查i % 3 == 0

暂无
暂无

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

相关问题 致命错误:未捕获错误:无法使用PDOStatement类型的对象作为数组 - Fatal error: Uncaught Error: Cannot use object of type PDOStatement as array FATAL ERROR未捕获错误:无法使用stdClass类型的对象作为数组 - FATAL ERROR Uncaught Error: Cannot use object of type stdClass as array 致命 Wordpress 错误:致命错误:未捕获错误:无法使用 WP_Error 类型的 object 作为数组 - Fatal Wordpress Error: Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in 致命错误:无法将类型的对象用作数组 - Fatal Error : Cannot use object of type as array PHP 致命错误:未捕获的错误:无法使用 stdClass 类型的 object 作为数组 - PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array 致命错误:未捕获错误:无法在 C:\xampp\htdocs\MyShoppingList\list.php:487 中使用类型为 stdClass 的 object 作为数组 - Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\MyShoppingList\list.php:487 致命错误:不能将MongoCursor类型的对象用作数组 - Fatal error: Cannot use object of type MongoCursor as array 致命错误:无法使用 SplFileInfo 类型的对象作为数组 - Fatal error: Cannot use object of type SplFileInfo as array 致命错误:无法将mysqli_result类型的对象用作数组 - Fatal error: Cannot use object of type mysqli_result as array 致命错误:无法将模板类型的对象用作数组 - Fatal error: Cannot use object of type Template as array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM