Php 构建阵列时如何个性化索引

Php 构建阵列时如何个性化索引,php,arrays,foreach,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="deta

你好

初级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>
哪个输出

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$标签

}

if (array_key_exists("5", $array)) {
//获取elementid returnlabeland并解析它以从url获取图像id

$parts = Explode('/', $array[5]);
$returnlabel = $parts[count($parts) - 2];
//echo$returnlabel

}
如果使用快捷方式
[]
表示法,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($shipping->{'links'}->{'link'}作为$link){ $array=当前($link->attributes())

foreach($array as$attributes=>$value){
//echo$attributes=>$value;
“$attributes=>$value”;
}
if(在_数组(“self”,$array)中){
回声“获得自我”;
$array['href'];
$parts=分解('/',$array['href']);
$shipmentid=$parts[计数($parts)-1];
echo$shipmentid;
}
if(在_数组中(“详细信息”、$array)){
echo“获得详细信息”;
$parts=分解('/',$array['href']);
$shipmentdetails=$parts[计数($parts)-2];
echo$shipmentdetails;
}
if(在_数组中(“price”,$array)){
回声“得到了价格”;
$parts=分解('/',$array['href']);
$shipmentprice=$parts[计数($parts)-2];
echo$shipmentprice;
}
if(在_数组(“label”,$array)中){
echo“得到了labelId”;
$parts=分解('/',$array['href']);
$shipmentartifact=$parts[计数($parts)-2];
echo$shipmentartifact;
}
if(在_数组(“returnLabel”,“$array”)中){
echo“得到了returnlabelId”;
$parts=分解('/',$array['href']);
$returnlabel=$parts[计数($parts)-2];
echo$returnlabel;
}   
回声';
打印(数组);
回声';
}
}
回声';
回声';
回声';
回声';
回声';
回声';

如何获取关键字?在你的问题之后,这一大块代码的目的是什么?xml还是。如何使用这些键构建数组,并将每个键的值设置为url,这样看起来就像myarray=[self=>,price=>;后面的代码块是我现在的状态,它查看数组,对于索引1执行此操作,对于索引2执行此操作。我更希望索引self执行此操作,如果索引详细信息执行此操作。它解析href中的字符串并捕获图像id,在本例中为“12345/0”捕获“12345”从url。理想情况下,我更喜欢引用self,而不是1,在我的条件下引用details insread 2。我如何实现这一点?我的问题中是否有足够的信息?理想情况下,它将容纳我的if-sdstatement,即如果self存在,则执行此操作,如果details存在,则执行此操作。如何正确构建数组,以便键索引包含这些关键字instead个数字。关键字来自xml,其中rel=self是关键字ah。您希望最终得到
$array['self']
$array['details']
,等等,其中每个都包含该组的所有链接?其中一个数组中的self值为xml中的href。$myarray['self'=>,['details'=>。因此我的if条件我可以使用self或details if existI只是想将上面的xml转换为一个数组。该数组将包含返回的每个链接,这些链接标识为self、detail、price、artifact,并且每个url都有一个来自xml的值,也就是href=然后我可以设置一个类似if(array\u key\u exists)的条件(“self”,$array)){…..我可以这样做吗?数组([0]=>SimpleXMLElement对象([self]=>)
$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 ($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>';