PHP - cannot use a scalar as an array warning

PhpArraysZend FrameworkWarningsScalar

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

$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'];

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.

Solution 5 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzozoView Question on Stackoverflow
Solution 1 - Phpbrian_dView Answer on Stackoverflow
Solution 2 - PhpLanView Answer on Stackoverflow
Solution 3 - PhpPeace NgaraView Answer on Stackoverflow
Solution 4 - PhpAllyn OView Answer on Stackoverflow
Solution 5 - PhpBenjaminView Answer on Stackoverflow