Home >Backend Development >PHP Tutorial >为什么用$$定义成数组后,会报错Cannot use string offset as an array
分别有数组$tongji1,$tongji2,$tongji3,$tongji4,$tongji5,$tongji6,$tongji7,$tongji8
结构类似为
$tongji1=Array
(
[kc1] => Array
(
[zong] =>2
[rs] =>3
)
[kc2] => Array
(
[zong] =>4
[rs] =>5
)
)
$tongji2=Array
(
[kc1] => Array
(
[zong] =>4
[rs] =>5
)
[kc3] => Array
(
[zong] =>1
[rs] =>2
)
)
……
数组里包含的简明现在想把几个数组中拥有同一kc*的给合并起来,用以下代码
$tongjiz=$tongji1;
for ($x=2; $x $z="tongji".$x;
foreach($$z as $k=>$v){
if(array_key_exists($k,$tongjiz)){
$tongjiz[$k][zong]=$tongjiz[$k][zong]+$$z[$k][zong];
}else{
$tongjiz[$k]=$$z[$k];
}
}
}
现在到$tongjiz[$k][zong]=$tongjiz[$k][zong]+$$z[$k][zong];就出错
发现是因为$$z[$k][zong]这个问题,认为不是一个数组
但我单独print_r($$z)的时候能输出数组的
用print_r($$z[$k])就开始不行了
请问这是为什么啊 应该怎么解决 谢谢
$$z[$k]['zong']
要写作
${$z}[$k]['zong']
$$z[$k]['zong']
要写作
${$z}[$k]['zong']