我对以下代码有疑问。变量 $entry 永远不会大于 1。我希望它增加以能够获取所有关键字并将它们放在一个变量中。我找不到 $entry 增加的原因。提前致谢!:-)
function objectsIntoArray($arrObjData, $entry, $arrSkipIndices = array()) {
`$arrData = array();`
$kwords=array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if ($index=="keywordterm"&&$index!="0"){
$kword=$arrObjData[$index];
//echo "arrObjData[$index]: ".$kword."</br></br>";
$kwords[$entry]=$kword;
//echo "keywords: ".$kwords."</br></br>";
//echo "keywords[$entry]: ".$kwords[$entry]."</br></br>";
$entry++;
}
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $entry, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
//echo "$arrData[$index]: ".$arrData[$index]."</br>";
}
}
return $arrData;
}
`$entry=0;
$xmlUrl = "9424.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj, $entry);`
第一次执行时显示:关键字[0]:电信计算
第二个显示:关键字[0]:多代理系统
你看?又是0....
xml中的一些代码:
<keywordset keywordtype="Inspec">
<keyword>
<keywordterm><![CDATA[telecommunication computing]]></keywordterm>
</keyword>
<keyword>
<keywordterm><![CDATA[multi-agent systems]]></keywordterm>
</keyword>
<keyword>
<keywordterm><![CDATA[state estimation]]></keywordterm>
</keyword>
<keyword>
<keywordterm><![CDATA[control engineering computing]]></keywordterm>
</keyword>
<keyword>
<keywordterm><![CDATA[telecommunication control]]></keywordterm>
</keyword>
</keywordset>