Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中对左连接查询返回的数组数据进行分组?_Php_Mysql_Join - Fatal编程技术网

如何在php中对左连接查询返回的数组数据进行分组?

如何在php中对左连接查询返回的数组数据进行分组?,php,mysql,join,Php,Mysql,Join,我有以下两个表,其值如下: tbl_品牌 id名称 1苹果 2三星 tbl_产品 id品牌idp\u名称 11移动电话 21耳塞 32移动式 这里当我使用左连接查询即 从'tbl_品牌'b'中选择'b.'id'作为'brand_id','b.'name'作为'brand_name','p.'p_name'作为'product_name',从'tbl_品牌'b'中选择'b.'id'作为'p'上的'tbl_产品'p'。'brand_id'='b.'id' 然后打印结果,我得到以下数组: Arr

我有以下两个表,其值如下:


tbl_品牌

id名称

1苹果

2三星


tbl_产品

id品牌idp\u名称

11移动电话

21耳塞

32移动式


这里当我使用左连接查询

从'tbl_品牌'b'中选择'b.'id'作为'brand_id','b.'name'作为'brand_name','p.'p_name'作为'product_name',从'tbl_品牌'b'中选择'b.'id'作为'p'上的'tbl_产品'p'。'brand_id'='b.'id'

然后打印结果,我得到以下数组:

Array
    (
        [0] => stdClass Object
            (
                [brand_id] => 1
                [brand_name] => Apple
                [product_name] => Mobile
            )
        [1] => stdClass Object
            (
                [brand_id] => 1
                [brand_name] => Apple
                [product_name] => Earpods
            )
        [2] => stdClass Object
            (
                [brand_id] => 2
                [brand_name] => Samsung
                [product_name] => Mobile
            )
一切正常。但我想要的结果是这样的:

Array
    (
        [0] => stdClass Object
            (
                [brand_id] => 1
                [brand_name] => Apple
                [product_name] => stdClass Object
                               (
                                   [0] => Mobile
                                   [1] => Earpods
                               )
            )
        [1] => stdClass Object
            (
                [brand_id] => 2
                [brand_name] => Samsung
                [product_name] => Mobile
            )

我想根据
品牌id
键对数据进行分组。我该怎么做呢?

虽然您可以在一个循环中生成所需的数组,但我宁愿执行两个查询。首先将所有品牌提取到一个数组中,并向每个品牌添加一个空产品数组。然后取出所有产品并分配给相关品牌

因为我不知道您使用的是什么DB库,下面是一些伪代码:

$data = [];

$brandResult = $db->query("SELECT id, name FROM tbl_brand");
while ($row = $brandResult->fetchObject()) {
    $row->product_names = [];
    $data[$row->id] = $row;
}

$productResult = $db->query("SELECT id, brand_id, p_name FROM tbl_products");
while ($row = $productResult->fetchObject()) {
    $data[$row->brand_id][$row->id] = $row->p_name;
}

mysql返回行,所以如果您想要一个多维数组,您必须自己构造它。沿着这些线进行循环即可:

$array=array();
foreach ($result as $row) {
    $array[$row->brandid]['brandid'] = $row->brandid;
    $array[$row->brandid]['brand_name'] = $row->brand_name;
    $array[$row->brandid]['products'][] = $row->product;
    }

你能把数组作为PHP代码发布吗?使用
var\u导出($myArray)