This piece of code will only work if $$val
is already an array. If it is not it will generate that error.
array_push($$val,$pieces[1]);
So try this :-
foreach($chunk as $key => $value)
{
$pieces = explode("=",$value);
if($pieces)
{
$val = $pieces[0];
if( isset($$val) && is_array($$val) ) {
array_push($$val,$pieces[1]);
} else {
$$val = array();
array_push($$val,$pieces[1]);
}
}
}
Of course it would be easier to use the $_GET array which PHP prepares automatically for you
Additional thought after your comment
Could the reason for this warning be that you are using an old version of PHP and have the parameter register_globals = on
.
So assuming a url of www.xx.com?q=1&q=2&q=3
This would mean that PHP had already created a $q
variable for you. So the first pass through your code would find all the variables in the query string already exists as strings i.e. PHP will have already created the variables like this invisibly to you.
$q = '1';
$q = '2';
$q = '3';
So the first time your code attempts the array_push($$val,$pieces[1]);
$$val
which equates to $q
but $q
is already a scalar string variable, hence your error message.
You can check if this is the case by adding
print_r($GLOBALS);
just before the section of your script you posted. Then check the url against variables that already exist.
Another thought
The only way to explain this warning is that one or more of the variables you are trying to create in this code snippet already exists, for whatever reason.
Its quite possible that this code worked originally without the warning, but since then the code was amended and someone created one or more or there variables for some temporary purpose earlier on in the code.
As your querystring parameters equate to variables like
cid, val, np, wgt, w, l, h
Its quite possible someone used $w or $l or $h especially for some temporary purpose within a loop to hold a value. Its common practice to use single letter variables names to denote that the variable is only used within the few lines that you can see on a screen. But uncommon for people to destroy them once they are no longer required.
Anyway its worth having a look to see if one or more of these variable have been created prior to your code snippet.
Add a
print_r($GLOBALS)
just before your code snippet and see if any of these variables already exist.