8

I am trying this code:

$rescntryvals[] = $rescntry;
$rescntry = "";
$resclkvalscntry[] = $rclick;
$rclick = "";
$resclkaddsnm[] = $addsnmame;
$addsnmame = "";

But I get this:

warning: Cannot use a scalar value as an array

Why? And what is the solution?

Cleb
  • 25,102
  • 20
  • 116
  • 151
jeevan
  • 165
  • 2
  • 4
  • 7

4 Answers4

9

You have to declare $rescntryvals as array before. Per default all variables are of type null (undefined) until you define them.

$rescntryvals  = array();
$rescntryvals[]=$rescntry;
djot
  • 2,952
  • 4
  • 19
  • 28
dev.meghraj
  • 8,542
  • 5
  • 38
  • 76
5

Try this :

Declare the variables

$rescntryvals  = array();
$rescntryvals[]=$rescntry;

OR

$rescntryvals  = array($rescntry);

Ref: http://php.net/manual/en/language.types.array.php

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
2

in first line, define your variables which supposed to be an array.

$rescntryvals     = array();

$resclkvalscntry  = array();

$resclkaddsnm     = array();
0

As per the PHP documentation, scalar values hold one item at a time. Arrays hold a list of items.

Ensure that your code is creating an array rather than one item.

OP's code is not initializing an array as shown by the accepted answer.

It can happen, for example, when you accidentally assign a boolean value to a variable that should be an array, just like I did in the following code:

$arr = asort($arr);
Dharman
  • 30,962
  • 25
  • 85
  • 135
earth2jason
  • 715
  • 1
  • 9
  • 20