我对下面的代码有问题。变量$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..。
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>发布于 2012-07-15 07:04:09
我不完全确定您在这里试图实现什么,但我注意到的一件事是,您试图在没有引用对象的情况下更改参数。
变化
function objectsIntoArray($arrObjData, $entry, $arrSkipIndices = array()) { 至
function objectsIntoArray(&$arrObjData, &$entry, $arrSkipIndices = array()) { 它可能会有帮助,也可能没有帮助;但这是值得尝试的东西。
举个例子。
function IncNumber($num)
{
$num++;
}
$num = 0;
IncNumber($num);
// $num will still be 0
// Using & to declare a reference to the object
function IncNumber2(&$num)
{
$num++;
}
IncNumber2($num);
// $num will be 1https://stackoverflow.com/questions/11487998
复制相似问题