我刚开始学习Laravel,我有一个查询结果如下:

[
  {
    servicename: "test",
    thing: "1"
  },
  {
    servicename: "test",
    thing: "2"
  },
  {
    servicename: "test",
    thing: "3"
  },
  {
    servicename: "test2",
    thing: "4"
  },
  {
    servicename: "test2",
    toothnum: "5"
  },
  {
    servicename: "test2",
    thing: "6"
  }
]

有没有可能把它变成:
  {
    services: {
      test:[
         "1",
         "2",
         "3"
       ]
      test2:[
         "4",
         "5",
         "6"
       ]
    }

  }

基本上,我希望结果集可以在我的视图中循环,这样我就可以显示每个服务的“东西”。

最佳答案

你可以这样循环数组:

$output_arr = array();
foreach($your_arr as $val)
{
 $output_arr[$val['servicename']][] = $val['thing'];
}

print_r($output_arr);

10-08 03:48