0

再会,

这里的初级 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;

}
4

2 回答 2

0
$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-11T19:26:18.503 回答
0

美好的一天,我修补了一下并弄清楚了,这是一个很棒的练习,感谢您提供帮助我理解一些事情的提示

foreach ($shipment->{'links'}->{'link'} as $link) { $array = current($link->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>';
于 2013-10-20T22:41:47.453 回答