再会,
这里的初级 PHP 人,我正在尝试从 Web 服务返回的答案构建一个数组。
从 Web 服务返回的答案提供了这样的 xml 响应。
<links>
<link rel="self" href="https://XX/rs/111111111/2222222222/shipment/347881315405043891" media-type="application/vnd.cpc.shipment-v4+xml"></link>
<link rel="details" href="https://XX/rs/111111111/2222222222/shipment/347881315405043891/details" media-type="application/vnd.cpc.shipment-v4+xml"></link>
<link rel="group" href="https://XX/rs/111111111/2222222222/shipment?groupid=bobo" media-type="application/vnd.cpc.shipment-v4+xml"></link>
<link rel="price" href="https://XX/rs/111111111/2222222222/shipment/347881315405043891/price" media-type="application/vnd.cpc.shipment-v4+xml"></link>
<link rel="label" href="https://XX/ers/artifact/11111111/5555555/0" media-type="application/pdf" index="0"></link>
</links>
我正在尝试从 xml 构建一个数组
foreach ($shipment->{'links'}->{'link'} as $link) {
//for each shipment go throught the loop and build array
$array[] = $link->attributes()->{'rel'};
//$array[] = $link->attributes()->{'href'};
}
print_r($array);
哪些输出
Array ( [0] => SimpleXMLElement Object ( [0] => self ) [1] => SimpleXMLElement Object ( [0] => details ) [2] => SimpleXMLElement Object ( [0] => group ) [3] => SimpleXMLElement Object ( [0] => price ) [4] => SimpleXMLElement Object ( [0] => label ) )
理想情况下,我如何将键设为“rel =”,以便在我的 if 语句中我实际上可以使用关键字而不是数字?
//如果数组中的elementid存在,则执行操作
if (array_key_exists("4", $array)) {
//grab the elementid label and parse it to grab image id from the url
$parts = Explode('/', $array[4]);
$label = $parts[count($parts) - 2];
//回显$标签;
}
if (array_key_exists("5", $array)) {
//获取 elementid 返回标签并解析它以从 url 中获取图像 id
$parts = Explode('/', $array[5]);
$returnlabel = $parts[count($parts) - 2];
//回显 $returnlabel;
}