gpt4 book ai didi

php - 把 Laravel Query 变成 Key Value?

转载 作者:行者123 更新时间:2023-11-29 03:26:15 26 4
gpt4 key购买 nike

我刚刚开始学习 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com