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?
The D language supports scalar arrays, which correspond directly in concept and syntax with arrays in C. A scalar array is a fixed-length group of consecutive memory locations that each store a value of the same type.
PHP includes two main types of variables: scalar and array. Scalar variables contain only one value at a time, and array variables contain a list of values. Array variables are discussed in the next section. PHP scalar variables contain values of the following types: Integers: whole numbers or numbers without decimals.
Definition: A scalar valued function is a function that takes one or more values but returns a single value. f(x,y,z) = x2+2yz5 is an example of a scalar valued function. A n-variable scalar valued function acts as a map from the space Rn to the real number line. That is, f:Rn->R.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With