Php 查询结果并显示在网页上(嵌套数组)

Php 查询结果并显示在网页上(嵌套数组),php,arrays,multidimensional-array,twig,Php,Arrays,Multidimensional Array,Twig,表结构 $query = $this->db->query("SELECT * FROM coupon c INNER JOIN coupon_customer cc ON c.coupon_id = cc.coupon_id LEFT JOIN coupon_store cs ON c.coupon_id = cs.seller_store_id LEFT JOIN seller_store ss ON c.seller_store_id = ss.seller_store_

表结构

   $query = $this->db->query("SELECT * FROM coupon c INNER JOIN coupon_customer cc ON c.coupon_id = cc.coupon_id LEFT JOIN coupon_store cs ON c.coupon_id = cs.seller_store_id LEFT JOIN seller_store ss ON c.seller_store_id = ss.seller_store_id WHERE cc.customer_id = $customer_id AND c.date_end > NOW() ");

       if ($query->num_rows) {
            return $query->rows;
        } else {
            return 0;
        }
一切都很好,但我想问的是,是否存在“将同一家卖家商店分组”的问题? 图:(来自我的代码的结果)

我想要的是:


如果是同一个存储,那么它将分组在一起,而不是重新显示存储名称的新行,假设查询结果集是这样的(为了简洁起见,我删除了大部分字段):

$rows=[
['seller\u store\u id'=>1,'seller\u store\u name'=>Foodlama','seller\u id'=>11,'coupon\u id'=>6322],
['seller\u store\u id'=>2,'seller\u store\u name'=>BlueFood Market','seller\u id'=>33,'coups\u id'=>555],
['seller\u store\u id'=>2,'seller\u store\u name'=>BlueFood Market','seller\u id'=>33,'coups\u id'=>7787],
];
然后,您可以重新格式化结果集以按存储分组:

$reformatted=[];
foreach($行作为$行){
//这里我们得到了所有其他不用于分组的键=>值对
$nonStoreInfo=array\u filter($row,function($key){
返回$key!='seller\u store\u id'&&&key!='seller\u store\u name'&&key!='seller\u id';
},数组\过滤器\使用\键);
/*
*我们必须手动添加该组常见的任何数据。
*
*在这里,我们用每次迭代覆盖它,以避免不必要的条件语句
*(检查键是否存在并且是否有值)。这样更简洁。
*这并不重要,因为不管怎么说,每个群体都是一样的。
*/
$reformatted[$row['seller\u store\u id']['seller\u store\u name']=$row['seller\u store\u name'];
$reformatted[$row['seller\u store\u id']['seller\u id']=$row['seller\u id'];
$reformatted[$row['seller\u store\u id']['coups'][]=$nonStoreInfo;
}
请注意,我对
卖家id
进行了猜测。如果任何
卖家店铺id
都可能不同,那么您必须将其从
数组过滤器
回调中删除,并删除手动分配
$reformatted[$row['seller\u store\u id']['seller\u id']=$row['seller\u id']
。同样,如果需要向组中添加内容,则必须在
array\u filter
中添加键比较并添加手动分配

这将最终输出如下所示的数组:

数组(
[1] =>阵列(
[卖家\店铺\名称]=>Foodlama
[卖方id]=>11
[优惠券]=>阵列(
[0]=>阵列(
[优惠券id]=>6322
)
)
)
[2] =>阵列(
[卖家商店名称]=>蓝色食品市场
[卖方id]=>33
[优惠券]=>阵列(
[0]=>阵列(
[优惠券id]=>555
)
[1] =>阵列(
[优惠券id]=>7787
)
)
)
)

现在,您可以在模板中迭代顶层(存储)然后在商店内迭代优惠券。您可以根据需要在组中使用数组结构和所需的确切数据。

将查询结果重新格式化为嵌套数组,该数组将在单个商店名称下包含优惠券集合,并将其传递给查看。您知道如何重新格式化吗?有问题吗在这个问题上,如果我不知道确切的结构,我也帮不了你。你能在尝试重新格式化的同时把它添加到这个问题上吗?@El_Vanja,我已经在我的问题上添加了表格结构。你需要分组的卖家的共同属性是什么?是否
'seller\u store\u name'
只与一个
'seller\u store\u id'
绑定?什么out
'seller\u id'
?你打算在模板中使用所有这些东西吗?现在我在做很多猜测。嗨,感谢你的解释,通过查看数组,是的,这是我需要的样式。但是由于我对编程技能的知识有限,我想问一个愚蠢的问题,那是为了$REF吗formatted和foreach,我应该放在哪里?在
if($query->num_rows)
中,然后返回
$reformatted
,而不是原始结果集。
   $query = $this->db->query("SELECT * FROM coupon c INNER JOIN coupon_customer cc ON c.coupon_id = cc.coupon_id LEFT JOIN coupon_store cs ON c.coupon_id = cs.seller_store_id LEFT JOIN seller_store ss ON c.seller_store_id = ss.seller_store_id WHERE cc.customer_id = $customer_id AND c.date_end > NOW() ");

       if ($query->num_rows) {
            return $query->rows;
        } else {
            return 0;
        }
Table: coupon
 coupon_id(PK)   name   coupon_code  date_start   date_end  



Table: coupon_customer 
coupon_id(FK)    customer_id(FK)



Table: coupon_store
 coupon_store_id(PK)    coupon_id(FK)    seller_store_id(FK)



Table: seller_store
 seller_store_id(PK)    seller_store_name    seller_id(FK)

 Table: seller
 seller_id(FK)     seller_name   seller_email

 Table: customer
 customer_id(PK)    customer_name customer_email