请参见下面的XML主体:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://start.exactonline.be/api/v1/393999/sync/logistics/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">SalesItemPrices</title>
<link rel="self" title="SalesItemPrices" href="SalesItemPrices" />
<entry>
<id>https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117417L)</id>
<title type="text"></title>
<updated>2021-08-11T11:47:22Z</updated>
<author>
<name />
</author>
<link rel="edit" title="SalesItemPriceSync" href="SalesItemPrices(2781117417L)" />
<category term="Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:ItemCode>OX11213001-M61</d:ItemCode>
<d:Price m:type="Edm.Double">136.36</d:Price>
</m:properties>
</content>
</entry>
<entry>
<id>https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117419L)</id>
<title type="text"></title>
<updated>2021-08-11T11:47:22Z</updated>
<author>
<name />
</author>
<link rel="edit" title="SalesItemPriceSync" href="SalesItemPrices(2781117419L)" />
<category term="Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:ItemCode>EK1117005.58</d:ItemCode>
<d:Price m:type="Edm.Double">164.46</d:Price>
</m:properties>
</content>
</entry>
</feed>
我需要得到每个条目的值:、ItemCode& Price。
我试图这样做:$data =$data,但是当我执行print_r($print_r->entry)时,我得到了以下内容:
SimpleXMLElement Object
(
[id] => https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117417L)
[title] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => text
)
)
[updated] => 2021-08-11T13:47:01Z
[author] => SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
)
)
[link] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => edit
[title] => SalesItemPriceSync
[href] => SalesItemPrices(2781117417L)
)
)
[category] => SimpleXMLElement Object
(
[@attributes] => Array
(
[term] => Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync
[scheme] => http://schemas.microsoft.com/ado/2007/08/dataservices/scheme
)
)
[content] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => application/xml
)
)
)
但我希望在“内容”部分中有:OX11213001-M61 136.36。
那么,如何才能得到这些值呢?还有,如何正确地在所有这些“条目”之间做一个循环?
我尝试了foreach ($products->entry as $product),但是收到了错误:警告:为foreach()提供的无效参数
发布于 2021-08-11 14:51:21
以下几点似乎奏效了。请注意,您的元素是命名空间-d,并且有前缀。
$simpleXML = new SimpleXMLElement($xml);
$myResult = [];
foreach($simpleXML->entry as $entry) {
$properties = $entry->content->children('m', true);
$myResult[] = ['itemCode' => (string) $properties->children('d', true)->ItemCode, 'itemPrice' => (string) $properties->children('d', true)->Price];
}
print_r($myResult); // to see the values
https://stackoverflow.com/questions/68743460
复制相似问题