douyu9159 2014-09-09 05:59
浏览 49
已采纳

通过PHP数组迭代为每个项添加整数索引

I have a result array being returned from a search function in one of the models. Everything works fine, however, I have recently added an iteration block to redact a row of data based on an ID. Like so:

for($i=0;$i<count($searchResponse);$i++){
    if($searchResponse[$i]['id']==$user_id){
        unset($searchResponse[$i]);
    }
}   

The code works fine and removes the appropriate data correctly. However it changes the array structure from this:

 "data":[
{
"id":"1",
"name_first":"Cameron",
"name_last":"Leafe",
"industry":"IT",
"workplace":"Leafe Interactive",
"imageURL":"https:\/\/scontent-b-kul.xx.fbcdn.net\/hphotos-xfa1\/t31.0-8\/p720x720\/819309_4979120151286_1476247244_o.jpg"
},
{
"id":"8",
"name_first":"Caroline",
"name_last":"McCullough",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
},
{
"id":"11",
"name_first":"Akiko",
"name_last":"McDougall",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
}
]

to this:

"data":{
"1":{
"id":"3",
"name_first":"Jane",
"name_last":"Doe",
"industry":"Retail - Fashion",
"workplace":"Big-W",
"imageURL":"http:\/\/www.johndoe.pro\/img\/John_Doe.jpg"
},
"5":{
"id":"8",
"name_first":"Caroline",
"name_last":"McCullough",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
},
"6":{
"id":"11",
"name_first":"Akiko",
"name_last":"McDougall",
"industry":"IT",
"workplace":"Occupation",
"imageURL":"http:\/\/www.mothersdelight.com\/avatar\/avatar.jpg"
}
}

Note the added index for each item in the second example. This small change causes all sorts of problems client side, PHP is not my normal area and I am not sure why this block is changing the structure. Usually I would work around this on the client side, however, the product is already deployed.

Any insights or suggestions on how to prevent the iteration block from causing this integer index to appear would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • duanniesui6391 2014-09-09 06:42
    关注

    You can use array_splice to manipulate non-associative arrays safely:

    for($i=0;$i<count($searchResponse);$i++){
        if($searchResponse[$i]['id']==$user_id){
            array_splice($searchResponse, $i, 1);
        }
    }
    

    array_splice takes the arguments

    array_splice( $array, $offset, $length*, $replacementArr* )
    

    with the second two arguments being optional. In this case, we want to remove one element from the array at index $i, so we use

    array_splice( $searchResponse, $i, 1 );
    

    Splicing can also be used to chop up arrays or replace array elements with another array, so it's a handy function to know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答