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?
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:
this above will work absolutely fine when used like:
But the below will throw a warning ::
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.
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
You need to set
$final[$id]
to an array before adding elements to it. Intiialize it with eitheror