如何在构建数组时个性化索引

时间:2013-10-11 19:19:13

标签: php arrays foreach

美好的一天,

初级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];

// echo $ label;

}

if (array_key_exists("5", $array)) {

//抓住elementid returnlabeland解析它以从网址

中获取图片ID
$parts = Explode('/', $array[5]);
$returnlabel = $parts[count($parts) - 2];

// echo $ returnlabel;

}

2 个答案:

答案 0 :(得分: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 ...
}

答案 1 :(得分:0)

美好的一天,我修好并想出来,这是一个很棒的练习,谢谢你帮我理解了一些事情的提示

foreach($ shipment-&gt; {'links'} - &gt; {'link'} as $ link){                                     $ array = current($ link-&gt; 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>';