PHP - 不能使用标量作为数组警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6019853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:09:57  来源:igfitidea点击:

PHP - cannot use a scalar as an array warning

phparrayszend-frameworkwarningsscalar

提问by zozo

I have the following code:

我有以下代码:

 $final = array();
    foreach ($words as $word) {
        $query = "SELECT Something";
        $result = $this->_db->fetchAll($query, "%".$word."%");
        foreach ($result as $row)
        {
            $id = $row['page_id'];
            if (!empty($final[$id][0]))
            {
                $final[$id][0] = $final[$id][0]+3;
            }
            else
            {
                $final[$id][0] = 3;
                $final[$id]['link'] = "/".$row['permalink'];
                $final[$id]['title'] = $row['title'];
            }
        } 
    }

The code SEEMS to work fine, but I get this warning:

代码似乎工作正常,但我收到此警告:

Warning: Cannot use a scalar value as an array in line X, Y, Z (the line with: $final[$id][0] = 3, and the next 2).

Can anyone tell me how to fix this?

谁能告诉我如何解决这个问题?

回答by brian_d

You need to set$final[$id]to an array before adding elements to it. Intiialize it with either

您需要先设置$final[$id]为数组,然后再向其添加元素。用任何一个初始化它

$final[$id] = array();
$final[$id][0] = 3;
$final[$id]['link'] = "/".$row['permalink'];
$final[$id]['title'] = $row['title'];

or

或者

$final[$id] = array(0 => 3);
$final[$id]['link'] = "/".$row['permalink'];
$final[$id]['title'] = $row['title'];

回答by Lan

A bit late, but to anyone who is wondering why they are getting the "Warning: Cannot use a scalar value as an array" message;

有点晚了,但是对于那些想知道为什么他们会收到“警告:不能将标量值用作数组”消息的人来说;

the reason is because somewhere you have first declared your variable with a normal integer or string and then later you are trying to turn it into an array.

原因是因为您首先使用普通整数或字符串声明变量,然后尝试将其转换为数组。

hope that helps

希望有帮助

回答by Peace Ngara

The Other Issue I have seen on this is when nesting arrays this tends to throw the warning, consider the following:

我在这方面看到的另一个问题是嵌套数组时这往往会引发警告,请考虑以下事项:

$data = [
"rs" => null
]

this above will work absolutely fine when used like:

上面的这个在使用时绝对可以正常工作:

$data["rs"] =  5;

But the below will throw a warning ::

但下面会发出警告::

$data = [
    "rs" => [
       "rs1" => null;
       ]
    ]
..

$data[rs][rs1] = 2; // this will throw the warning unless assigned to an array

回答by Allyn O

Also make sure that you don't declare it an array and then try to assign something else to the array like a string, float, integer. I had that problem. If you do some echos of output I was seeing what I wanted the first time, but not after another pass of the same code.

还要确保您没有将其声明为数组,然后尝试将其他内容分配给数组,例如字符串、浮点数、整数。我有那个问题。如果你做一些输出的回声,我第一次看到了我想要的东西,但不是在另一遍相同的代码之后。

回答by Benjamin

Make sure that you don't declare it as a integer, float, string or boolean before. http://php.net/manual/en/function.is-scalar.php

确保之前没有将其声明为整数、浮点数、字符串或布尔值。 http://php.net/manual/en/function.is-scalar.php