gpt4 book ai didi

php - 需要帮助分组 php 数组

转载 作者:行者123 更新时间:2023-12-05 00:07:35 25 4
gpt4 key购买 nike

我有一个 mysql 查询,它输出以下数组:

Array
(
[0] => stdClass Object
(
[item1] => foo 0
[item2] => bar 0
[catid] => 3
)

[1] => stdClass Object
(
[item1] => foo 1
[item2] => bar 1
[catid] => 7
)

[2] => stdClass Object
(
[item1] => foo 2
[item2] => bar 2
[catid] => 10
)

[3] => stdClass Object
(
[item1] => foo 3
[item2] => bar 3
[catid] => 7
)

)

我想知道是否可以按“类别”对数组进行分组,这样就不会出现重复的 catid,每个对象都会有一个 catid。这是否有意义?所以说 catid 7 只会出现一次,里面有两个 item1/item2。

最佳答案

这个怎么样?

foreach($results as $result) {
$by_category[$result->catid][] = $result;
}

print_r($by_category);

关于php - 需要帮助分组 php 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3766100/

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