php - 在php和mysql中创建多维数组

标签 php mysql ajax

目前我正在尝试使用两个查询(项目和类别)在 php 中创建一个多维数组。 我在该网站上进行了搜索,但没有找到与我要查找的内容类似的内容。 如果有人可以帮助我使用我的代码,我将不胜感激。

请查看我正在寻找的内容和我的代码。

表格:

TABLE Items;

+-----------------------------------+
|    id    |    type    |    name   |
+----------+------------+-----------+
|     1    |     4      |  item_1   |       
|     2    |     3      |  item_2   |
|     3    |     2      |  item_3   |
+-----------------------------------+

TABLE Categories;

+-----------------------------------+
|    id    |   Item_id  |    name   |
+----------+------------+-----------+
|     1    |     2      |   Cat_a   |       
|     2    |     2      |   Cat_b   |
|     3    |     3      |   Cat_x   |
|     4    |     3      |   Cat_z   |
|     5    |     3      |   Cat_b   |
|     6    |     1      |   Cat_b   |
|     7    |     3      |   Cat_y   |
+-----------------------------------+

我正在寻找的结果:

Array
(
    [0] => Array
        (
            id   => 1
            name => Item_1
            Type => 4
            cats => Array
                (
                    [6] => Cat_b
                )
        )
    [1] => Array
        (
            id   => 2
            name => Item_2
            Type => 3
            cats => Array
                (
                    [1] => Cat_a
                    [2] => Cat_b
                )
        )
    [2] => Array
        (
            id   => 3
            name => Item_3
            Type => 2
            cats => Array
                (
                    [3] => Cat_x
                    [4] => Cat_z
                    [5] => Cat_b
                    [7] => Cat_y
                )
        )
)

我的代码:

$result = mysqli_query($link, "SELECT * FROM Categories WHERE Item_id = '233'");


foreach ($result as $key => $value) {
    $v[] = $value["id"];
}

    foreach ($v as $key => $res) {
        $query = mysqli_query($link, "SELECT * FROM Items WHERE category_id = '".$res."'");
           foreach ($query as $k =>$att){
                 $var[$res][] =  $att["name"]; 
          }
    }

    echo '<pre>' . print_r($var,1) . '</pre>';  

最佳答案

使用以下内容,已测试且有效

$sql = "SELECT i.id,i.name,i.type,c.id AS CatKey,c.name As catName FROM `Items` AS i JOIN Categories AS c ON i.id=c.Item_id ORDER BY i.id ASC ";
$result = $conn->query($sql);
$res = array();
$i = 0;
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       $ids = array_column($res, 'id');
       if(in_array($row['id'], $ids)){
            $res[$i-1]['cats'][$row['CatKey']] = $row['catName'];
       }else{
        $res[$i]['id']   = $row['id'];
        $res[$i]['type'] = $row['type'];
        $res[$i]['name'] = $row['name'];
        $res[$i]['cats'] = array($row['CatKey'] => $row['catName']);
        $i++;
       }
    }
} 
echo '<pre>';
print_r($res);

结果:-

Array
(
    [0] => Array
        (
            [id] => 1
            [type] => 4
            [name] => item_1
            [cats] => Array
                (
                    [6] => Cat_b
                )

    )

[1] => Array
    (
        [id] => 2
        [type] => 3
        [name] => item_2
        [cats] => Array
            (
                [1] => Cat_a
                [2] => Cat_b
            )

    )

[2] => Array
    (
        [id] => 3
        [type] => 2
        [name] => item_3
        [cats] => Array
            (
                [3] => Cat_x
                [4] => Cat_z
                [5] => Cat_b
                [7] => Cat_y
            )

    )

)

关于php - 在php和mysql中创建多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54865198/

相关文章:

php - Octobercms - 按变量排序记录,如果值相同,则按第二个变量排序

javascript - 在 PHP 和 JavaScript 中打印月份名称而不是月份编号

mysql - Node 数据库查询不工作;

php - 如何使用 select st 在 mysql 中同时获取值并继续执行下一个递增 id

php - 可以在 session 中切换 PHP session 吗?

php - 禁止下载 PHP 文件

mysql - 如何根据每个用户给定的记录集获取最后 5 或 6 条记录

php - 如何在 MySQL Strict 中测试 PHP

php - 使用 Ajax 和 Laravel 5.6 上传多个图像

jquery - 通过 AJAX 提交的表单不会序列化所有组件,为什么?