次のスニペットでは、特定の親で最初のサブ要素を取得し、それを配列に変換するよりも、php のシンプルな xml オブジェクト フィードを使用して xml からノードを取得します。私の問題:穴のXMLオブジェクトに最も多くの子を持つサブ要素を取得したいので、ターゲットを配列に変換する必要があります
/*
* Retrive XML Nodes from Feed
*
*/
public function getXMLNodes() {
$xml = simplexml_load_file($this->config[0]->link);
switch (strtolower($this->config[0]->affiliate)) {
case 'case1':
$nodes = $xml->product[0]; //here stead of first object I should get that one which has most of chlidren
break;
case 'case2':
$nodes = $xml->productItems->productItem[0];
break;
default :
break;
}
$nodes = $this->xml2array($nodes);
return $nodes;
}
/*
* Convert Simple XML Object to array
*
* @xml SimpleXMLObject
*
*/
function xml2array($xml) {
$arr = array();
foreach ($xml as $element) {
$tag = $element->getName();
$e = get_object_vars($element);
if (!empty($e)) {
$arr[$tag] = $element instanceof SimpleXMLElement ? $this->xml2array($element) : $e;
} else {
$arr[$tag] = trim($element);
}
}
return $arr;
}
XML の例 //この場合、2 番目に最も多くの子が含まれます
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name></name>
<productUrl></productUrl>
<warranty/>
<availability></availability>
<inStock/>
<shippingCost></shippingCost>
<deliveryTime></deliveryTime>
<weight/>
<size/>
<brand/>
<model/>
<ean/>
<upc/>
<isbn/>
<condition></condition>
<mpn/>
<techSpecs/>
<manufacturer></manufacturer>
<programName></programName>
<programLogoPath></programLogoPath>
<programId></programId>
<fields>
<AlsoIncludes></AlsoIncludes>
<Carrier></Carrier>
<modelID></modelID>
<Offertype></Offertype>
<Processor></Processor>
<Service></Service>
<ShippingTax></ShippingTax>
<StandardImage155x155></StandardImage155x155>
<StandardImage200x200></StandardImage200x200>
<Systemtype></Systemtype>
</fields>
</product>
<product>
<name></name>
<productUrl></productUrl>
<imageUrl></imageUrl>
<description></description>
<price></price>
<currency></currency>
<merchantCategoryName></merchantCategoryName>
<sku></sku>
<shortDescription/>
<promoText/>
<previousPrice></previousPrice>
<warranty/>
<availability></availability>
<inStock/>
<shippingCost></shippingCost>
<deliveryTime></deliveryTime>
<weight/>
<size/>
<brand/>
<model/>
<ean/>
<upc/>
<isbn/>
<condition></condition>
<mpn/>
<techSpecs/>
<manufacturer>Dell</manufacturer>
<programName></programName>
<programLogoPath></programLogoPath>
<programId></programId>
<fields>
<AlsoIncludes></AlsoIncludes>
<Carrier></Carrier>
<modelID></modelID>
<Offertype></Offertype>
<Processor></Processor>
<Service></Service>
<ShippingTax></ShippingTax>
<StandardImage155x155></StandardImage155x155>
<StandardImage200x200></StandardImage200x200>
<Systemtype></Systemtype>
</fields>
</product>
</products>