基于键的组数组 [英] Group array based on key

查看:69
本文介绍了基于键的组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从数据库中获取的数组。这是返回的结果。

I have an array that is fetched from my database. This is the result returned.

Array
(
    [0] => stdClass Object
        (
            [id] => 4
            [productID] => bla123
            [rating] => 2
            [username] => 
            [image] => black-468-sir-corbett-9-400x400-imadxbb7hyhkgvpc.jpeg
        )

    [1] => stdClass Object
        (
            [id] => 5
            [productID] => 12xye
            [rating] => 4
            [username] => 
            [image] => black-602-unistar-5-400x400-imaeffn5mytbruum.jpeg
        )

    [2] => stdClass Object
        (
            [id] => 6
            [productID] => bla123
            [rating] => 2
            [username] => 
            [image] => black-468-sir-corbett-9-400x400-imadxbb7hyhkgvpc.jpeg
        )

    [3] => stdClass Object
        (
            [id] => 7
            [productID] => 12xye
            [rating] => 4
            [username] => 
            [image] => black-602-unistar-5-400x400-imaeffn5mytbruum.jpeg
        )
)

我想根据产品密钥对数据库进行分组。这样我就可以得到一个数组,其中包含产品ID及其对应的值,例如其图像和数组中的等级。这基本上是针对评级系统,产品及其评级的。

I would like to Group this data base on product key. So that I would get a an array with product id and its corresponding values like its images and its rating in an array. This is basically for an rating system, product and its rating.

如果以上操作不切实际,您能否建议我以某种方式显示所有带有其图像及其相应等级的产品,例如一颗星:1颗两颗星:3三颗星:2从上述数据中得出的其他信息。

If the above is not practical, can you please suggest me some way in which I can display all products with its image and its corresponding rating like one star:1 two star:3 three star:2 etc from the above data.

我尝试过这样

foreach($ratings as $value) {
           $productID = $value->productID;
           if(isset($result[$productID])){
            $result[$productID][] = $value->rating;
           } else {
                 $result[$productID] = array();
            }
        }

从上面的代码中我将得到

from the above code I will get

Array
(
    [bla123] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [12xye] => Array
        (
            [0] => 4
            [1] => 3
            [2] => 3
            [3] => 2
            [4] => 2
            [5] => 4
            [6] => 2
            [7] => 2
            [8] => 2
        )

)

问题是我还需要添加图像,我发现这样做确实很难。

The problem is I also need to add the image to this, which I am finding really hard to do.

推荐答案

使用以下代码,您将在关联数组内部获得结果,其中键为产品键。我认为这种解析方法比遍历数组的每个值,获取对象,获取ID并使用该信息设置新数组要简单。

With the following code, you will get the result inside of an associated array with the key as the product key. I think that this resolution is less complicated than going to every value of the array, get the object, get the ID and setting a new array with that info.

while ($row = mysqli_fetch_assoc($res)) {
    $array[$row[id]] = $row;
}

如果您仍然想使用它,那就这样做

If you want to use it anyway, then do this

foreach ($array as $val) {
    $array_res[$val->id] = $val;
}

这篇关于基于键的组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆