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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看