php 致命错误:无法取消设置字符串偏移错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12289714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Cannot unset string offsets error?
提问by Jared
Not sure why this is occurring: Basically, I have an array that contains the following arrays, see var_dump:
不知道为什么会这样:基本上,我有一个包含以下数组的数组,请参阅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 foreachto loop through the first array()and assign it's nested array values in the first array.
显然,这些数据有点多余,但它是使用 xpath 获取值时返回的数据。所以我正在做一个foreach循环遍历第一个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,
谢谢,
采纳答案by Tom
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 的工作方式感到困惑。
foreach($array as $key => $value)
{
echo $key;
echo $value;
}
will display the key and value for each key/value pair in an array.
将显示数组中每个键/值对的键和值。
回答by Steve Tauber
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 stringinstead of an array.
当 $search['param'] 仍然是string而不是array时,这会引发相同的错误。
回答by taz
I believe that you have the syntax for the foreachwrong...it should be $key => $valuewhere you have $key => $arr2. So when you have $arr2[$key]you are looking for element $keyin the nested array $arr2. $arr2is referenced by $key, which is either a string (for an associative array) or an integer (for a non-associative array). $arr2could also be referenced by $arr[$key].
我相信你有foreach错误的语法......它应该是$key => $value你有的地方$key => $arr2。因此,当您在嵌套数组$arr2[$key]中查找元素$key时$arr2。$arr2由 引用$key,它可以是字符串(对于关联数组)或整数(对于非关联数组)。$arr2也可以参考$arr[$key]。
回答by Enzo Apollonio
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
$arr2[$key]成为字符串"ivr"并且$arr2[$key][0]是字符串的第一个字符并且不能取消设置
回答by user2275693
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 ...

