PHP - Cannot Use a Scalar as an Array Warning

Fatal error: Uncaught Error: Cannot use a scalar as an array warning

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'];

Cannot use a scalar value as an array

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;

Warning: Cannot use a scalar value as an array in

You can try declaring the variable $result, as an array, before using it.

$result = array();
// Loop through and create a result array, with the key being even, the result, odd
for($i = 0; $i < count($array)/2; $i++) {

$result[$array[$i*2]] = $array[$i*2+1];
}

cannot use a scalar value as an array warning

You must declare $voted as array before increasing the index. You are trying to push a new element to the array with a complex structure (could be a scalar value as well), if the json data returns a scalar, you should not be able to push using $voted[].

$voted = array();

Profiler for Sql CE

The only tested solution I know of that could solve this problem is Altiris Profiler which is a tool I designed at my previous job, but is closed source and not-for-sale.

The way you would hook it in, is by creating a factory for your commands and proxing them for profiling purposes before using them (using RealProxy). Its really light weight and about 10 lines of code to implement.

On my question Flory talks about a new tool called dynaTrace that may also be able to solve this problem as well.

warning:Cannot use a scalar value as an array

First you are defining $_SESSION['userpasswordmatch']=true;. true is a scalar value. Then $_SESSION['userpasswordmatch']['name']=$db_name; where you are treating $_SESSION['userpasswordmatch'] as an array.

You can simply do $_SESSION['userpasswordmatch']['name']=$db_name; and echo it.

No need for setting it to true. This would work -

$abc=array();
$abc['name']=$db_name; //When i echo this one.It does echo the name i.e. 'Tilak'
$_SESSION['userpasswordmatch']['name']=$abc['name'];
echo $_SESSION['userpasswordmatch']['name'];

Update

No need to define a blank array. When you are setting $_SESSION['userpasswordmatch']['name'] = $db_name;, $_SESSION['userpasswordmatch'] is already defined as an array.

$_SESSION['userpasswordmatch']['check']= true; // use this for that check

PHP Error - Cannot use a scalar value as an array

You are trying to use $result as an array when it is already initialized as a boolean. You could reinitialize it as an array using:

$result = array('level' => $this->model_user->users_level($username));

or

$result = array();
$result['level'] = $this->model_user->users_level($username);

However, this is a bad idea since you are using the same variable for different things. A better solution is to rename one of the variables e.g.

$logged_in = $this->model_user->login($username,$password);
if ($logged_in == true){

Or better yet, since the boolean is only being used once, you can skip the initialization of $logged_in and use the result of $this->model_user->login($username,$password) directly in the condition

if ($this->model_user->login($username,$password)) {

You can omit the == true since it returns a boolean.

Cannot Use a Scalar Value as an Array, But Data Successfully Updated

Note: I hope you require $res = $user->updateSegmentGender ('303', $segment_gender); need $data value and based on assumption I use $data for return part.

Perhaps due to $data not initialized. I'm trying to declaring the variable $data, as an array, before using it I also make another change that function updateSegmentGender() require return part so I put this.

public function updateSegmentGender ($product_id, $segment_gender) {

$this->connect();
$product_id = $this->escapeString($product_id);

$row_count = count($segment_gender);

$data = array();//Initialize variable...
for ($row = 0; $row < $row_count; $row++) {

$this->select('product_seg_gender', '*', NULL,
'product_id = "'.$product_id.'" AND gender = "'.$segment_gender[$row][0].'"', NULL, NULL);
$res = $this->getResult();
$res = count($res);

$gender = $segment_gender[$row][0];
$status = $segment_gender[$row][1];

if ($res <> 0) {
// UPDATE
$data = array ('status'=>$status);
$this->update('product_seg_gender', $data, 'product_id = "'.$product_id.'" AND gender = "'.$gender.'"');
}else {
// INSERT
$data = array ('product_id'=>$product_id, 'gender'=>$gender, 'status'=>$status);
$this->insert('product_seg_gender', $data);
}
}
//Return funal value in array format...
return $data;
}


Related Topics



Leave a reply



Submit