PHP - cannot use a scalar as an array warning PHP - cannot use a scalar as an array warning arrays arrays

PHP - cannot use a scalar as an array warning


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


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


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