0

我有以下问题:

我无法将所有关键字连接到一个字符串中。它返回一个数组......我尝试了几个小时,但我没有管理它。

这是代码:

(if 条件在循环中)

$keyword="";
$entry=0;

if ($index=="keywordterm){
            $kword=$arrObjData[$index]; // takes the value of keywordterm
            $kwords[$entry]=$kword;
            echo "keywords[$entry]: ".$kwords[$entry]."</br></br>";
            $keywordd.=$kwords[$entry]." | ";   
            $entry++;           
}
print_r($keywordd);
echo "Keywords: ".$keywordd."<br><br>";

结果是:

keywords[1]: telecommunication computing

keywords[2]: multi-agent systems

keywords[3]: state estimation

keywords[4]: control engineering computing

keywords[5]: telecommunication control

keywords[6]: Matlab-Simulink platform

keywords[7]: networked multiagent systems

keywords[8]: information sharing

keywords[9]: multiple cooperative agents

keywords[10]: networked control system

Array ( )
Keywords: Array

我想要这样的东西:

Keywords: telecommunication computing | multi-agent systems | state estimation.........

XML 代码如下:

<keywordset keywordtype="Inspec">
      <keyword>
        <keywordterm><![CDATA[computer aided manufacturing]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[semantic Web]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[factory automation]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[ontologies (artificial intelligence)]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[knowledge based systems]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[inference mechanisms]]></keywordterm>
      </keyword>
    </keywordset>
    <keywordset keywordtype="InspecFree">
      <keyword>
        <keywordterm><![CDATA[reconfigurable manufacturing systems]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[semantic Web services]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[factory automation]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[ontologies]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[explicit semantics]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[logical reasoning]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[autonomous manufacturing orchestration]]></keywordterm>
      </keyword>
      <keyword>
        <keywordterm><![CDATA[knowledge based systems]]></keywordterm>
      </keyword>
    </keywordset>

在此先感谢,并对这个 loooong 代码感到抱歉..

所有的代码都是这样的:

$keywordd="";
$entry=0;
$kwords=array();
$xmlUrl = "9424.xml"; // XML feed file/URL

$xmlStr = file_get_contents($xmlUrl);

$xmlObj = simplexml_load_string($xmlStr);

$arrXml = objectsIntoArray($xmlObj, $entry, $keywordd);

function objectsIntoArray(&$arrObjData, &$entry, &$keywordd, $arrSkipIndices = array()) {

if (is_array($arrObjData)) {

    echo "<pre>";

        //print_r($arrObjData);
        echo "</pre>";  

        foreach ($arrObjData as $index => $value) {

if ($index=="keywordterm"&&$index!="0"){
            $kword=$arrObjData[$index];
            echo "<b>KEYWORD: </b>".$kword."</br></br>";
            $kwords[$entry]=$kword;
            echo "keywords[$entry]: ".$kwords[$entry]."</br></br>";
            $keywordd.=$kwords[$entry]." | ";
            //echo $keywordd."<br><br>";
            $entry++;
            //echo "entry: ".$entry."<br>";

        }
        elseif ($index=="joursec"&&$index!="0"){
            //print_r($keywordd);
            echo "<b>Keywords: </b>".$keywordd."<br><br>";
        }
}
}
4

2 回答 2

1

由于我看到 $keywordd 是一个字符串,所以你不能用 print_r 打印它吗?如果你只做 "echo $keywordd" 会发生什么?

您应该使用 implode() 将数组转换为字符串吗?

例子:

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
于 2012-07-16T18:14:31.600 回答
0

您是否尝试过使用implode

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(" | ", $array);
于 2012-07-16T18:21:03.123 回答