douwei8295 2019-02-25 11:28
浏览 55
已采纳

在php和mysql中创建多维数组

Currently I am trying create an array multidimensional in php using two queries (Items and Categories). I made a search in the site but I did not found anything similar to what I am looking for. I appreciate if someone can help me using my code.

Please se bellow what am looking for and my code.

Tables:

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   |
+-----------------------------------+

Result that I am looking for:

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
                )
        )
)

My code:

$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>';  
  • 写回答

3条回答 默认 最新

  • doumaqing6652 2019-02-25 12:46
    关注

    Use the following, Tested and working

    $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);
    

    Result:-

    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
                )
    
        )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)