美好的一天,
这里的初级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 语句中我实际上可以使用关键字而不是数字?
如果数组中的元素 ID 存在,则执行操作
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];
回声$label;
}
if (array_key_exists("5", $array)) {
获取元素 ID 返回标签并解析它以从 URL 中获取图像 ID
$parts = Explode('/', $array[5]);
$returnlabel = $parts[count($parts) - 2];
回声$returnlabel;
}
$array['rel'] = $link->attributes()->{'rel'};
如果您使用快捷键[]
表示法,PHP 将仅使用下一个更高的未使用数字索引来创建新的数组元素。如果您想要下一个序列号以外的其他东西,则必须自己提供。
请注意,在编写时,上面的代码将简单地用当前迭代覆盖上一次迭代的 rel。
评论跟进
好的,你会想要这样的东西,而不是:
$array = array();
foreach ($shipment->{'links'}->{'link'} as $link) {
$rel = $link->getAttribute('rel');
$array[$rel][] = $link;
}
然后稍后你可以执行以下操作:
foreach ($array['self'] as $link) {
$href = $link->getAttribute('href');
... do something with the href ...
}
美好的一天,我捣鼓并弄清楚了,这是一个很好的练习,感谢您的提示,帮助我理解了一些事情
foreach ($shipment->{'links'}->{'link'} as $link) { $array = 当前($link->属性());
foreach ($array as $attributes => $value) {
//echo $attributes => $value;
"$attributes => $value";
}
if (in_array("self", $array)) {
echo "Got self";
$array['href'];
$parts = Explode('/', $array['href']);
$shipmentid = $parts[count($parts) - 1];
echo $shipmentid;
}
if (in_array("details", $array)) {
echo "Got details";
$parts = Explode('/', $array['href']);
$shipmentdetails = $parts[count($parts) - 2];
echo $shipmentdetails;
}
if (in_array("price", $array)) {
echo "Got price";
$parts = Explode('/', $array['href']);
$shipmentprice = $parts[count($parts) - 2];
echo $shipmentprice;
}
if (in_array("label", $array)) {
echo "Got labelId";
$parts = Explode('/', $array['href']);
$shipmentartifact = $parts[count($parts) - 2];
echo $shipmentartifact;
}
if (in_array("returnLabel", $array)) {
echo "Got returnlabelId";
$parts = Explode('/', $array['href']);
$returnlabel = $parts[count($parts) - 2];
echo $returnlabel;
}
echo '<pre>';
print_r($array);
echo '</pre>';
}
}
echo '<form action="GetShipment.php" method="GET"><input type="hidden" name="shipmentid" value="' . $shipmentid . '"/><input type="submit" value="Get Shipment" /></form>';
echo '<form action="/../GetShipmentDetails/GetShipmentDetails.php" method="POST"><input type="hidden" name="shipmentdetails" value="' . $shipmentdetails . '"/><input type="submit" value="Get Shipment Details" /></form>';
echo '<form action="/../GetShipmentPrice/GetShipmentPrice.php" method="POST"><input type="hidden" name="shipmentprice" value="' . $shipmentprice . '"/><input type="submit" value="Get Shipment Price" /></form>';
echo '<form action="/../GetShipmentArtifact/GetShipmentArtifact.php" method="POST"><input type="hidden" name="shipmentartifact" value="' . $shipmentartifact . '"/><input type="submit" name="artifactidfake" value="Print Shipping label"/></form>';
echo '<form action="/../GetShipmentArtifact/GetShipmentArtifact.php" method="POST"><input type="hidden" name="returnlabel" value="' . $returnlabel . '"/><input type="submit" name="artifactidfake" value="Return Shipping label/Commercial invoice "/></form></td>';
echo '<form action="/../VoidShipment/VoidShipment.php" method="POST"><input type="hidden" name="shipmentid" value="' . $shipment->{'shipment-id'} . '"/><input type="submit" name="Try me" value="Void Shipment" /></form></td>';