我刚刚开始学习 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"
]
}
}
基本上我希望结果集是我可以在我的 View 中循环的东西,这样我就可以显示每个服务的“东西”。
最佳答案
你可以像这样循环你的数组来做到这一点:
$output_arr = array();
foreach($your_arr as $val)
{
$output_arr[$val['servicename']][] = $val['thing'];
}
print_r($output_arr);
关于php - 把 Laravel Query 变成 Key Value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35910985/