dsgw8802 2017-08-25 02:55
浏览 244
已采纳

Laravel 5.4数组删除键索引

In my controller, this statement generates an array:

// sort the region collection by continent for faster access in front end
$finalRegions = $regions->sortBy('continent_id');


{  
   "0":{  
      "id":1,
      "name":"Alaska",
      "image_x":227,
      "image_y":117
   },
   "1":{  
      "id":5,
      "name":"Australian Antartic Territory",
      "image_x":1187,
      "image_y":1037
....
   }
}

How do I remove the index from the resulting object, so it looks like this:

[  
   {  
      "id":1,
      "name":"Alaska",
      "image_x":227,
      "image_y":117
   },
   {  
      "id":5,
      "name":"Australian Antartic Territory",
      "image_x":1187,
      "image_y":1037
....
   }
]

This is stored in a field cast as json in the table class.

  • 写回答

3条回答 默认 最新

  • dongqin1075 2017-08-25 03:19
    关注
    $res = [];
    foreach ($finalRegions  as $key => $value) {
        $res[] = $value;
    }
    
    // $res contains desired result
    

    Edit: Simple one liner (thanks to Jannie)

    $res = array_values($finalRegions)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料