基于关键PHP集团数组 [英] Group array based on key PHP

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

问题描述

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

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();
            }
        }

从上面的code I将获得

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

推荐答案

通过以​​下code,你将获得与密钥产品密钥关联数组内的结果。我认为这个分辨率比去到阵列的每个值那么复杂,获取对象,获取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;
}

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

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