我面对以下XML:
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_PURCHASE_VALUTA
[valid_from] => 2015-12-14 13:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 17.0266
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.326
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.4771
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 26.0743
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 19.5504
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_SALE_DEVIZA
[valid_from] => 2015-12-14 13:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 18.1262
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.6161
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.7017
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 27.5739
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 20.813
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
[valid_from] => 2015-12-14 13:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 17.34581
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.46043
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.54232
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 26.46609
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 19.91697
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_MIDDLE
[valid_from] => 2015-12-14 13:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 17.736
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.53827
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.622
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 27.02
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 20.365
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_SALE_VALUTA
[valid_from] => 2015-12-12 00:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 18.3578
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.6435
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.7488
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 27.8306
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 21.0322
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_PURCHASE_VALUTA
[valid_from] => 2015-12-12 00:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 17.0275
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.3251
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.4771
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 26.0743
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 19.5082
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_SALE_DEVIZA
[valid_from] => 2015-12-12 00:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 18.1272
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.6152
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.7017
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 27.5739
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 20.7681
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
[valid_from] => 2015-12-12 00:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 17.34679
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.45955
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.54232
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 26.46609
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 19.87394
)
)
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => XML_RATE_TYPE_EBNK_MIDDLE
[valid_from] => 2015-12-12 00:00:00.0
)
[currency] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => AUD
[quota] => 1
[rate] => 17.737
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => HRK
[quota] => 1
[rate] => 3.53737
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => DKK
[quota] => 1
[rate] => 3.622
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => EUR
[quota] => 1
[rate] => 27.02
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => JPY
[quota] => 100
[rate] => 20.321
)
)
)
)
我根本不喜欢XML。我一直使用JSON。我需要从XML返回2个数组。必须听取具体情况。
我只需要今天类型有效的[currency]
数组。像
[valid_from] => date('Y-m-d')
XML的在线URL:
我需要两个数组:
@type:XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
@valid_from:2015-12-12 00:00:00.0
和
@type:XML_RATE_TYPE_EBNK_SALE_DEVIZA
@valid_from:2015-12-12 00:00:00.0
答案 0 :(得分:1)
您可以改为使用DomDocument
- http://php.net/manual/en/class.domdocument.php
它允许您读取xml并选择其节点
// Example
$dom = new DomDocument();
$dom->loadXML( file_get_contents($path) ); // $path -> where your xml saved
// it has methods get element by tag name
$tag = $dom->getElementsByTagName("some_tag_name");// it will return array
// it has methods get element attributees
$tag->item(0)->getAttribute('some_attribute'); // you can take 0 index and read its attribute
// to take tag value you can
$tag->item(0)->item(0)->nodeValue
// ...
如果这个基本示例还不够,如果你可以在你的uestion中发布你的xml,我可以尝试帮助你创建精确的命令
<强>更新强>
$currencyArr1 = array();
$currencyArr2 = array();
$dom = new DomDocument();
$dom->loadXML( file_get_contents('http://104.236.115.118/application/currency_values/RB_Exchange_list.xml') );//$path path to your xml
$exchangeRates = $dom->getElementsByTagName("exchange_rate");
foreach ($exchangeRates as $exchangeRate){
$validFrom = $exchangeRate->getAttribute('valid_from');// Your date
$type = $exchangeRate->getAttribute('type');// Your type
//First array case
//@type:XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
//@valid_from:2015-12-12 00:00:00.0
if( $type == "XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA" &&
date('Y-m-d H:i:s', strtotime($validFrom)) == date('Y-m-d H:i:s', strtotime("2015-12-12 00:00:00")) ){
$currencyes = $exchangeRate->getElementsByTagName("currency");
foreach ($currencyes as $currency){
$currencyArr1[] = array(
"name" => $currency->getAttribute("name"),
"quota" => $currency->getAttribute("quota"),
"rate" => $currency->getAttribute("rate"),
);
}
}
//Second array case
//@type:XML_RATE_TYPE_EBNK_SALE_DEVIZA
//@valid_from:2015-12-12 00:00:00
if( $type == "XML_RATE_TYPE_EBNK_SALE_DEVIZA" &&
date('Y-m-d H:i:s', strtotime($validFrom)) == date('Y-m-d H:i:s', strtotime("2015-12-12 00:00:00")) ){
$currencyes = $exchangeRate->getElementsByTagName("currency");
foreach ($currencyes as $currency){
$currencyArr2[] = array(
"name" => $currency->getAttribute("name"),
"quota" => $currency->getAttribute("quota"),
"rate" => $currency->getAttribute("rate"),
);
}
}
}
echo "ARRAY1:"; var_dump($currencyArr1);
echo "<br /><br />ARRAY2:"; var_dump($currencyArr2);
答案 1 :(得分:0)
如果您对使用SimpleXML感兴趣,则应使用xpath
function。
例如,理论上这应该让你遍历所有名为&#34; exchange_rate&#34;的元素。与type=XML_RATE_TYPE_EBNK_SALE_VALUTA
$content->xpath('exchange_rate[type="XML_RATE_TYPE_EBNK_SALE_VALUTA"]');
然后,您可以遍历结果并比较不同属性的值(通过attribute
property找到)。