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?
This question is related to
php
arrays
zend-framework
warnings
scalar
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
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
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.
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
Source: Stackoverflow.com