Fatal error: Cannot unset string offsets error?
I believe that you have the syntax for the foreach
wrong...it should be $key => $value
where you have $key => $arr2
. So when you have $arr2[$key]
you are looking for element $key
in the nested array $arr2
. $arr2
is referenced by $key
, which is either a string (for an associative array) or an integer (for a non-associative array). $arr2
could also be referenced by $arr[$key]
.
http://php.net/manual/en/control-structures.foreach.php
You don't need the unset, you are overriding the outer parameters with the value of the inner array as opposed to the whole array.
$a1 = array("ivr");
$a2 = array("ivr2");
$a3 = array($a1, $a2);
foreach($a3 as $key => $value){
$a3[$key] = $a3[$key][0];
//unset($arr2[$key][0]);
};
var_dump($a3);
I think you are confused about how foreach works.
foreach($array as $key => $value)
{
echo $key;
echo $value;
}
will display the key and value for each key/value pair in an array.
This happens when you try to unset a string value - In below case you access the first element in the array which is a string and the try to unset it which causes this error
$a=array("hello", "there");
unset($a[0][0]);
This causes:
Fatal error: Cannot unset string offsets in ... on line ...
I had this error in a slightly different situation which might prove useful.
unset($search['param']['complete'])
This threw the same error when $search['param'] was still a string instead of an array.