Fatal error: Cannot unset string offsets error?
Asked Answered
B

6

20

Not sure why this is occurring: Basically, I have an array that contains the following arrays, see var_dump:

array(2) { 
  [0]=> array(1) { 
    [0]=> string(3) "ivr" 
  } 
  [1]=> array(1) { 
    [0]=> string(9) "ivr_dests" 
  } 
}

Obviously this data is kind of redundant, but it's what was returned while getting values with xpath. So I'm doing a foreach to loop through the first array() and assign it's nested array values in the first array.

Basically, it should return this:

array(2) {
  [0]=> string(3) "ivr"
  [1]=> string(9) "ivr_dests"
}

So here is what I've setup:

foreach($arr as $key => $arr2){
    $arr2[$key] = $arr2[$key][0];
    unset($arr2[$key][0]); //This returns Fatal error: Cannot unset string offsets
//if I comment out the unset(), $arr[$key] returns the same value as it did (multidim array)
};

        //I tried this too:
$i=0;
foreach($arr as $arr2){
  $arr2[$i] = $arr2[$i][0];
  $i++;
}

Any ideas what I'm doing wrong? Should I go about this another way?

Thanks,

Beattie answered 5/9, 2012 at 21:1 Comment(0)
J
11

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.

Jarlath answered 5/9, 2012 at 21:13 Comment(1)
That makes sense. Part of my confusion was that I unset an array earlier, but I realize now I was creating a new array and unsetting the old one, not overwriting one like this example. You're example helps me make sense of foreach() too, thanks!Beattie
H
11

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.

Hahnert answered 22/5, 2014 at 17:22 Comment(0)
C
3

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 ...
Casting answered 27/9, 2019 at 11:1 Comment(0)
G
2

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

Glabrescent answered 5/9, 2012 at 21:6 Comment(0)
S
1

The error is in the code.
After you assign

$arr2[$key] = $arr2[$key][0];

$arr2[$key] becomes the string "ivr" and $arr2[$key][0] is the first character of the string and can't be unset

Scutch answered 16/2, 2016 at 10:21 Comment(0)
N
0

Yes, Use only array key to unset, not it's value. Example:

$fruits = ['a' => 'apple', 'b' => 'orange', 'c' => 'banana'];

if you try unset($fruits['a]['apple'];, you will face this error. Correct usage unset($fruits['a];

Nutty answered 23/6, 2021 at 5:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.