我正在尝试修复我刚接触的程序中的一些错误:
if (strtoupper($xmlnode["tag"])=="RANDOM"){
$liarray=array();
$children = $xmlnode["children"];
for ($randomc=0;$randomc<sizeof($children);$randomc++){
if (strtoupper($children[$randomc]["tag"]) == "LI"){
$liarray[]=$randomc;
}
}
在strtoupper($children[$randomc]["tag"])
我收到错误:
Warning: Illegal string offset 'tag'
为什么会发生这种情况,我该如何纠正?如果需要,我可以添加更多代码。
答案 0 :(得分:1)
您的$xmlnode['children']
是一个字符串,而不是一个数组。
它正在寻找像这样的结构:
$xmlnode['children'] = [
['tag' => 'LI'],
['tag' => 'LU'],
['tag' => 'LA'],
['tag' => 'LO'],
['tag' => 'LE'],
['tag' => 'LR'],
];
但实际上你给它的是$xmlnode['children'] = "I am a string";
编辑:完整答案:
首先需要检查$xmlnode['children']
数组中的当前项是否是数组,而不是字符串,然后只处理数组的键。
$xmlnode['tag'] = 'RANDOM';
$xmlnode['children'] = array(
" ",
array(
'tag' => 'li',
'attributes' => "",
'value' => "Tell me a story."
),
" ",
array(
'tag' => 'li',
'attributes' => "",
'value' => "Oh, you are a poet."
),
" ",
array(
'tag' => 'li',
'attributes' => "",
'value' => "I do not understand."
),
" "
);
$liarray = array();
if (strtoupper($xmlnode["tag"]) == "RANDOM") {
$children = $xmlnode["children"];
for ($randomc=0; $randomc < sizeof($children); $randomc++) {
if (is_array($children[$randomc])) {
if (strtoupper($children[$randomc]["tag"]) == "LI") {
$liarray[] = $randomc;
}
}
}
print_r($liarray);
}