doulaopu2343 2013-10-11 19:19 采纳率: 100%
浏览 16
已采纳

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

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;

}
  • 写回答

2条回答 默认 最新

  • douyouqian8550 2013-10-11 19:26
    关注
    $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 ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度