64

I have the following code:

 $final = [1 => 2];
 $id = 1;

 $final[$id][0] = 3;

The code seems to work fine, but I get this warning:

Warning: Cannot use a scalar value as an array in line X (the line with: $final[$id][0] = 3).

Can anyone tell me how to fix this?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
zozo
  • 8,230
  • 19
  • 79
  • 134
  • Reproducibles: [Cannot Use a Scalar Value as an Array, But Data Successfully Updated](https://stackoverflow.com/q/34916007/2943403) and [PHP Error - Cannot use a scalar value as an array](https://stackoverflow.com/q/31892296/2943403) – mickmackusa Dec 29 '22 at 08:53

4 Answers4

104

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'];
brian_d
  • 11,190
  • 5
  • 47
  • 72
  • 1
    You can simplify some lines in the first case to `$final[$id][] = 3;` and in the second to `$final[$id] = array(3);` :) – Tadeck May 16 '11 at 16:07
  • 2
    @Tadeck yes you can, I thought I would be explicit though ; ) – brian_d May 16 '11 at 16:46
  • 8
    This solution solved my problem, but the "why" of it eludes me. PHP, as far as I've ever used it, doesn't require you to declare a variable's type. My code was working fine, and then it last night started spewing this error for some inexplicable reason, out of the clear blue sky. As a matter of course, in my MVC (CodeIgniter) code I routinely assign the $data = array() type at the top of my functions. In my plain jane php, I rarely do. In php, I don't think it's mandatory. Yet, declaring my variable as an array type solved my problem. Perplexed. – TARKUS Jul 05 '16 at 10:56
95

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Lan
  • 1,874
  • 2
  • 20
  • 37
3

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
Peace Ngara
  • 673
  • 5
  • 7
  • 2
    I think in this case it's because you forgot to quote the `rs` and `rs1` on the last line. That makes them undefined constants, and so `$data[rs]` is not defined. – Doin Mar 20 '22 at 13:22
-1

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.

Allyn O
  • 15
  • 1