日安,
初级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];//回波$label;
}
if (array_key_exists("5", $array)) {//获取elementid返回区,将其解析为从url获取图像id
$parts = Explode('/', $array[5]);
$returnlabel = $parts[count($parts) - 2];//回波$returnlabel;
}发布于 2013-10-11 19:26:18
$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 ...
}发布于 2013-10-20 22:41:47
一天,我做了些调整,想明白了,这是一个很好的练习,谢谢你给我的提示,帮助我理解了一些事情。
foreach ($付运->{‘links’}->{‘link’} as $link) { $array =current($$array->attributes());
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>';https://stackoverflow.com/questions/19325657
复制相似问题