一起<;说明>;元素if<;特征类型>;元素在XML PHP解析中是相同的
xml.php 需要这样的输出吗一起<;说明>;元素if<;特征类型>;元素在XML PHP解析中是相同的,php,xml,xml-parsing,Php,Xml,Xml Parsing,xml.php 需要这样的输出吗 SimpleXMLElement Object ( [feature_type] => Copier Features [description] => Copier description1 Copier description2 ) SimpleXMLElement Object ( [feature_
SimpleXMLElement Object
(
[feature_type] => Copier Features
[description] => Copier description1
Copier description2
)
SimpleXMLElement Object
(
[feature_type] => Facsimile Features
[description] => Facsimile description1
Facsimile description2
)
这样做:
$xml = simplexml_load_string($xmlstr,'SimpleXMLElement', LIBXML_NOCDATA);
$x = count($xml->product_features->feature_item)-1;
$ft = ''; $d = '';
for ($x ; $x >= 0 ; $x--) {
if ($ft == (string)$xml->product_features->feature_item[$x]->feature_type) {
$xml->product_features->feature_item[$x]->description = $xml->product_features->feature_item[$x]->description . " $d";
} else {
$ft = (string)$xml->product_features->feature_item[$x]->feature_type;
$d = (string)$xml->product_features->feature_item[$x]->description;
unset($xml->product_features->feature_item[$x]);
}
}
echo "<pre>";
var_dump($xml);
echo "</pre>";
$xml=simplexml\u load\u字符串($xmlstr,'simplexmlement',LIBXML\u NOCDATA);
$x=计数($xml->product\u features->feature\u item)-1;
$ft=''$d='';
对于($x;$x>=0;$x--){
如果($ft==(字符串)$xml->product\u features->feature\u item[$x]->feature\u type){
$xml->product\u features->feature\u item[$x]->description=$xml->product\u features->feature\u item[$x]->description.“$d”;
}否则{
$ft=(字符串)$xml->product\u features->feature\u item[$x]->feature\u type;
$d=(字符串)$xml->product\u features->feature\u item[$x]->description;
取消设置($xml->product\u features->feature\u item[$x]);
}
}
回声“;
$xml = simplexml_load_file('xml.php','SimpleXMLElement', LIBXML_NOCDATA);
foreach($xml as $rows)
{
foreach($rows as $row){
echo "<pre>";
print_r($row);
echo "</pre>";
}
}
var_dump($xml);
回声“;
$xml = simplexml_load_file('xml.php','SimpleXMLElement', LIBXML_NOCDATA);
foreach($xml as $rows)
{
foreach($rows as $row){
echo "<pre>";
print_r($row);
echo "</pre>";
}
}
在描述之间添加空格。见现场演示:
$xml = simplexml_load_string($xmlstr,'SimpleXMLElement', LIBXML_NOCDATA);
$x = count($xml->product_features->feature_item)-1;
$ft = ''; $d = '';
for ($x ; $x >= 0 ; $x--) {
if ($ft == (string)$xml->product_features->feature_item[$x]->feature_type) {
$xml->product_features->feature_item[$x]->description = $xml->product_features->feature_item[$x]->description . " $d";
} else {
$ft = (string)$xml->product_features->feature_item[$x]->feature_type;
$d = (string)$xml->product_features->feature_item[$x]->description;
unset($xml->product_features->feature_item[$x]);
}
}
echo "<pre>";
var_dump($xml);
echo "</pre>";