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

Good day,

Junior PHP guy here, I am trying to build an array from an answer back from web service.

The answer back from web service provide an xml response as such.

<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>

I am trying to build an array out of the 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); 

WHICH OUTPUTS

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 ) ) 

Ideally How would i make the key the "rel=" so that in my if statement I could actually use the keyword instead of a number?

//if the elementid in the array exist do the action

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)) {

//grab the elementid returnlabeland parse it to grab image id from the url

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

//echo $returnlabel;

}
$array['rel'] = $link->attributes()->{'rel'};

If you use the shortcut [] notation, PHP will simply use the next higher unused numeric index available to create the new array element. If you want something other than that next-in-sequence number, you'll have to supply it yourself.

Note that as-written, the above code will simply overwrite the previous iteration's rel with the current iterations.


comment followup

Ok, you'd want something like this, instead:

$array = array();
foreach ($shipment->{'links'}->{'link'} as $link) {
    $rel = $link->getAttribute('rel');
    $array[$rel][] = $link;
}

Then later on you can do something like:

foreach ($array['self'] as $link) {
   $href = $link->getAttribute('href');
   ... do something with the href ...
}

Good day, i tinkered around and figured it out, this was a great exercise, thank you for the tip that helped me understand a few things

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>';