如何在PHP中基于某些分组将mysql查询的结果拆分为多个数组?

黄色小兵

我有php API(使用PDO)。我从客户端获取一些数据,并对其应用mysql查询,并从数据库接收查询结果。现在,我想要的是将查询结果拆分为多个数组(多维数组)。

就像Im将购物车的Item(11,12,13,17,17,18,19)ID发送到数据库查询一样,如下所示:

"SELECT s.SparePartID, s.Name, s.VendorID, v.CompanyName 
FROM sparepart s 
INNER JOIN users v
ON v.VendorID= s.VendorID
WHERE s.SparePartID IN ('11', '12','13', '17', '18', '19')"

以上查询结果如下:

+-------------+---------+----------+---------------------+
| SparePartID |   Name  | VendorID |     CompanyName     |
+=============+=========+==========+=====================+
|     11      |  Tyres  |    48    | Master Automotives  |
+-------------+---------+----------+---------------------+
|     12      |  Lights |    48    | Master Automotives  |
+-------------+---------+----------+---------------------+
|     13      |  Wheels |    48    | Master Automotives  |
+-------------+---------+----------+---------------------+
|     17      |  Torch  |    50    | *Repair Solutions*  |
+-------------+---------+----------+---------------------+
|     18      |   Lamp  |    50    | *Repair Solutions*  |
+-------------+---------+----------+---------------------+ 
|     19      | Camera  |    50    | *Repair Solutions*  |
+-------------+---------+----------+---------------------+

现在,我想它的保存群体的阵列其公司名称的基础上。因为此数据中2个不同的公司名称,所以我想将此数据保存在2个数组的数组中,否则组数应等于不同的公司名称。

因此,我希望将荡妇分成几个部分

[

"Group1": [

{
"SparePartID": "11",
"Name" : "Tyres",
"VendorID": "48",
"Company" : "Master Automotives"
},

{
"SparePartID": "12",
"Name" : "Lights",
"VendorID": "48",
"Company" : "Master Automotives"
},

{
"SparePartID": "13",
"Name" : "Wheels",
"VendorID": "50",
"Company" : "Master Automotives"
}

]


"Group2": [

{
"SparePartID": "17",
"Name" : "Torch",
"VendorID": "50",
"Company" : "*Repair Solutions*"
},

{
"SparePartID": "18",
"Name" : "Lamp",
"VendorID": "50",
"Company" : "*Repair Solutions*"
},

{
"SparePartID": "19",
"Name" : "Camera",
"VendorID": "50",
"Company" : "*Repair Solutions*"
}


]


]

这是我的show_cart.php的PHP API代码,我想对其进行修改以获得上述结果:

<?php

include_once '../config/database.php';
include_once '../objects/product.php';

$database = new Database();
$db = $database->getConnection();
$product = new Product($db);

// get posted data
$json = file_get_contents("php://input");
$data = json_decode($json, true);
// query products
$stmt = $product->cart($data);
$num = $stmt->rowCount();

// check if more than 0 record found
if($num>0){
    $products_arr=array();
    $products_arr["records"]=array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $product_item=array(
            "SparePartID" => $SparePartID,
            "Name" => $Name,
            "Price" => $Price,
            "VendorID" => $VendorID,
            "Company" => $Company
        );
        array_push($products_arr["records"], $product_item);
    }
    http_response_code(200);
    echo json_encode($products_arr);
} else{
    // set response code - 404 Not found
    http_response_code(404);
    echo json_encode(
        array("message" => "No Spare Parts found in shopping Cart ")
    );
}
?>

其中product.php代码为:

function cart($ids){
     $ids_arr = str_repeat('?,', count($ids) - 1) . '?';

     $query = "SELECT s.SparePartID, s.Name, s.VendorID, v.CompanyName FROM sparepart s INNER JOIN users v ON v.VendorID= s.VendorID WHERE s.SparePartID IN ({$ids_arr})";

    $stmt = $this->conn->prepare($query);
    // execute query
    $stmt->execute($ids);

    // return values from database
    return $stmt;
}

现在,请任何人帮助我建议如何根据公司名称拆分结果执行查询后,我应该修改查询还是修改结果数组吗?帮助

HTM地狱

只需更改此行:

array_push($products_arr["records"], $product_item);

对此:

$products_arr["records"][$product_item["Company"]][] = $product_item;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Pyspark:将多个数组列拆分为行

将大数组拆分为多个数组,其中php中出现空元素

根据名称将多个数组拆分为变量

如何基于定界符将数组列表拆分为两个数组列表?

Java-将数组数组拆分为多个数组

Python-将数组拆分为多个数组

将数组中的每个元素拆分为多个数组

PHP将数组上的foreach的某些结果分组到另一个数组中

如何在MySql PHP中拆分结果

如何通过在python xarray中添加额外的维度将一个数组拆分为多个数组?

如何基于值将多维数组拆分为数组-PHP

如何从2个数组中获取总和结果并拆分为多个输入?

如何使用Javascript将值拆分为多个数组?

Javascript:按值将数组拆分为多个数组

根据值将数组拆分为多个数组-PHP

如何基于PHP中的索引值将2D数组拆分为数组块

Javascript:将数组数组拆分为多个数组

将动态数组拆分为多个数组

将LINQ结果基于列拆分为数组

如何在Javascript中为每个索引将数组拆分为多个数组

python-如何在python中将多维数组拆分为多个单个数组?

如何将数组拆分为ID相同的Linq的多个数组

将数组拆分为多个数组并存储在不同的变量中

将 php 数组拆分为多个团队

将多维数组拆分为多个数组

JAVA:如何将数组从“ViewAll”是数组标题的值拆分为多个数组

将一个数组按键和值拆分为多个数组 PHP

基于行python中的公共标题将CSV文件拆分为多个数据框

如何根据给定数字增加值将数组拆分为多个数组?