我有一个数组,当我使用var_dump
显示数组时,我得到的输出是:
array(2) {
["System"]=>
array(2) {
[1]=>
string(1) "1111"
[2]=>
string(1) "2222"
[3]=>
string(1) "3333"
}
["test"]=>
string(2) "on"
}
我想使用foreach或任何将使用该代码的代码来访问每个内部值。我的意思是,例如,我想回声出这样的东西:
System "1" has "1111" value.
System "2" has "2222" value.
System "3" has "3333" value.
所以,我尝试使用这样的代码:
foreach ($array['System'] as $key => $value) {
echo 'System "' . $key . '" has "' . $value . '" value.'."<br />'n";
}
但是,这不是造字!
我有这个错误:
警告:非法的字符串偏移量"系统"
警告:为foreach()提供的参数无效
我的问题是什么?我该如何解决?
我无法更改数组的输出(当我使用var_dump时)
您的代码工作正常,如下所示:http://codepad.org/4mUtrJ6L
$array = array('System'=>array(
'1111',
'2222',
'3333'
));
foreach ($array['System'] as $key => $value) {
echo 'System "' . $key . '" has "' . $value . '" value.'."<br />'n";
}
输出:
System "0" has "1111" value.
System "1" has "2222" value.
System "2" has "3333" value.
你的数组应该这样构建:
$array = array('System'=>array('1111','2222','3333'));
同样在您的代码片段中,也没有类似Warning: Illegal string offset 'ans'
的内容。也许错误在其他地方