Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use a scalar value as an array

Tags:

php

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?

like image 524
jeevan Avatar asked May 22 '13 06:05

jeevan


People also ask

Can a scalar be an array?

A scalar array is a fixed-length group of consecutive memory locations that each store a value of the same type. You access scalar arrays by referring to each location with an integer starting from zero. In D programs, you would usually use scalar arrays to access array data within the operating system.

What is a scalar value in PHP?

Scalar variables are those containing an int, float, string or bool. Types array, object, resource and null are not scalar. Note: is_scalar() does not consider resource type values to be scalar as resources are abstract datatypes which are currently based on integers.

What's a scalar value?

A scalar value is simply a value that only has one component to it, the magnitude. For example, your speed is a scalar value because it only has one component, how fast you are going. Your height is also a scalar value because the only component is how tall you are. The same goes for your mass.

What is the difference between a scalar and an array?

There is one major difference between two of the main formats — scalar and array processors — in which a computer system processes information. While scalar processors work on one data item at a time, array processors can tackle multiple data streams simultaneously.


2 Answers

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;
like image 54
dev.meghraj Avatar answered Sep 20 '22 20:09

dev.meghraj


Try this :

Declare the variables

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

OR

$rescntryvals  = array($rescntry);

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

like image 40
Prasanth Bendra Avatar answered Sep 23 '22 20:09

Prasanth Bendra