【问题标题】:How can I display data from PHP arrays in a two-dimensional table?如何在二维表中显示 PHP 数组中的数据?
【发布时间】:2021-08-17 01:24:39
【问题描述】:

我从 DB 获得了三个数组,一个是健康机构名称数组,另一个是交货数据,最后一个是交货数据中日期的日期数组。例如:

Array ( 
[0] => stdClass Object ( [health_inst_code] => 32HI083 [health_inst_name] => DHM)
[1] => stdClass Object ( [health_inst_code] => 32HI084 [health_inst_name] => THQHSB) 
[2] => stdClass Object ( [health_inst_code] => 32HI085 [health_inst_name] => CHCM) 
[3] => stdClass Object ( [health_inst_code] => 32HI086 [health_inst_name] => THQHV) 
[4] => stdClass Object ( [health_inst_code] => 32HI087 [health_inst_name] => GHK) 
)



Array ( 
[0] => stdClass Object ( [dd_id] => 620 [delivery_date] => 2021-05-28 [health_inst_name] => DHM [delivery_institution] => 32HI083 [total_delivery] => 8 ) 
[1] => stdClass Object ( [dd_id] => 621 [delivery_date] => 2021-05-27 [health_inst_name] => THQHSB [delivery_institution] => 32HI084 [total_delivery] => 0 ) 
[2] => stdClass Object ( [dd_id] => 619 [delivery_date] => 2021-05-27 [health_inst_name] => GHK [delivery_institution] => 32HI087 [total_delivery] => 3 ) 
[3] => stdClass Object ( [dd_id] => 623 [delivery_date] => 2021-05-27 [health_inst_name] => CHCM [delivery_institution] => 32HI085 [total_delivery] => 0 ) 
[4] => stdClass Object ( [dd_id] => 625 [delivery_date] => 2021-05-27 [health_inst_name] => THQHV [delivery_institution] => 32HI086 [total_delivery] => 1 ) 
[5] => stdClass Object ( [dd_id] => 523 [delivery_date] => 2021-05-26 [health_inst_name] => THQHSB [delivery_institution] => 32HI084 [total_delivery] => 4 ) 
[6] => stdClass Object ( [dd_id] => 522 [delivery_date] => 2021-05-26 [health_inst_name] => CHCM [delivery_institution] => 32HI085 [total_delivery] => 1 ) 
[7] => stdClass Object ( [dd_id] => 521 [delivery_date] => 2021-05-26 [health_inst_name] => THQHV [delivery_institution] => 32HI086 [total_delivery] => 0 ) 
[8] => stdClass Object ( [dd_id] => 520 [delivery_date] => 2021-05-26 [health_inst_name] => GHK [delivery_institution] => 32HI087 [total_delivery] => 1 ) 
[9] => stdClass Object ( [dd_id] => 518 [delivery_date] => 2021-05-26 [health_inst_name] => DHM [delivery_institution] => 32HI083 [total_delivery] => 7 ) 
[10] => stdClass Object ( [dd_id] => 425 [delivery_date] => 2021-05-25 [health_inst_name] => THQHV [delivery_institution] => 32HI086 [total_delivery] => 1 ) 
[11] => stdClass Object ( [dd_id] => 424 [delivery_date] => 2021-05-25 [health_inst_name] => CHCM [delivery_institution] => 32HI085 [total_delivery] => 0 ) 
[12] => stdClass Object ( [dd_id] => 422 [delivery_date] => 2021-05-25 [health_inst_name] => DHM [delivery_institution] => 32HI083 [total_delivery] => 7 ) 
[13] => stdClass Object ( [dd_id] => 419 [delivery_date] => 2021-05-25 [health_inst_name] => THQHSB [delivery_institution] => 32HI084 [total_delivery] => 2 ) 
[14] => stdClass Object ( [dd_id] => 418 [delivery_date] => 2021-05-25 [health_inst_name] => GHK [delivery_institution] => 32HI087 [total_delivery] => 2 ) 
[15] => stdClass Object ( [dd_id] => 336 [delivery_date] => 2021-05-24 [health_inst_name] => GHK [delivery_institution] => 32HI087 [total_delivery] => 5 ) 
[16] => stdClass Object ( [dd_id] => 337 [delivery_date] => 2021-05-24 [health_inst_name] => DHM [delivery_institution] => 32HI083 [total_delivery] => 6 ) 
[17] => stdClass Object ( [dd_id] => 338 [delivery_date] => 2021-05-24 [health_inst_name] => CHCM [delivery_institution] => 32HI085 [total_delivery] => 0 ) 
[18] => stdClass Object ( [dd_id] => 340 [delivery_date] => 2021-05-24 [health_inst_name] => THQHV [delivery_institution] => 32HI086 [total_delivery] => 0 ) 
[19] => stdClass Object ( [dd_id] => 339 [delivery_date] => 2021-05-24 [health_inst_name] => THQHSB [delivery_institution] => 32HI084 [total_delivery] => 3 ) 
[20] => stdClass Object ( [dd_id] => 246 [delivery_date] => 2021-05-23 [health_inst_name] => GHK [delivery_institution] => 32HI087 [total_delivery] => 0 ) 
[21] => stdClass Object ( [dd_id] => 255 [delivery_date] => 2021-05-23 [health_inst_name] => THQHV [delivery_institution] => 32HI086 [total_delivery] => 1 ) 
[22] => stdClass Object ( [dd_id] => 253 [delivery_date] => 2021-05-23 [health_inst_name] => THQHSB Bathery [delivery_institution] => 32HI084 [total_delivery] => 4 ) 
[23] => stdClass Object ( [dd_id] => 251 [delivery_date] => 2021-05-23 [health_inst_name] => DHM [delivery_institution] => 32HI083 [total_delivery] => 4 ) 
[24] => stdClass Object ( [dd_id] => 249 [delivery_date] => 2021-05-23 [health_inst_name] => CHCM [delivery_institution] => 32HI085 [total_delivery] => 0 ) 
[25] => stdClass Object ( [dd_id] => 197 [delivery_date] => 2021-05-22 [health_inst_name] => DHM [delivery_institution] => 32HI083 [total_delivery] => 10 ) 
[26] => stdClass Object ( [dd_id] => 196 [delivery_date] => 2021-05-22 [health_inst_name] => THQHV [delivery_institution] => 32HI086 [total_delivery] => 0 ) 
[27] => stdClass Object ( [dd_id] => 193 [delivery_date] => 2021-05-22 [health_inst_name] => THQHSB [delivery_institution] => 32HI084 [total_delivery] => 2 ) 
[28] => stdClass Object ( [dd_id] => 194 [delivery_date] => 2021-05-22 [health_inst_name] => GHK [delivery_institution] => 32HI087 [total_delivery] => 1 ) 
[29] => stdClass Object ( [dd_id] => 195 [delivery_date] => 2021-05-22 [health_inst_name] => CHCM [delivery_institution] => 32HI085 [total_delivery] => 1 )
 )



Array ( 
[0] => stdClass Object ( [delivery_date] => 2021-05-28 ) 
[1] => stdClass Object ( [delivery_date] => 2021-05-27 ) 
[2] => stdClass Object ( [delivery_date] => 2021-05-26 )
[3] => stdClass Object ( [delivery_date] => 2021-05-25 ) 
[4] => stdClass Object ( [delivery_date] => 2021-05-24 ) 
[5] => stdClass Object ( [delivery_date] => 2021-05-23 ) 
[6] => stdClass Object ( [delivery_date] => 2021-05-22 ) 
)

我想在 PHP 中将数组显示为二维数组,如下所示:

             | 2021-05-28 |  2021-05-27 |  2021-05-26|  2021-05-25 |  2021-05-24 |2021-05-23|2021-05-22 |
|            |            |             |            |             |             |          |           |
---------------------------------------------------------------------------------------------------------
|    DHM     |    8       |      -      |     7      |      7      |      6      |    4      |    10
|   THQHSB   |    -       |      0      |     4      |      2      |      3      |    4      |     2
|   CHCM     |    -       |      0      |     1      |      0      |      0      |    0      |     1
|   THQHV    |    -       |      1      |     0      |      1      |      0      |    1      |     0
|   GHK      |    -       |      3      |     1      |      2      |      5      |    0      |     1

我该怎么做?

【问题讨论】:

    标签: php


    【解决方案1】:

    要获得您想要的两个暗淡数组,我们只需要第二个数组。

    <?php
    // example code
    
    $array2 = array ( 
    "0" => (object) array ( "dd_id" => 620, "delivery_date" => "2021-05-28", "health_inst_name" => "DHM", "delivery_institution" => "32HI083", "total_delivery" => 8 ) ,
    "1" => (object) array ( "dd_id" => 621, "delivery_date" => "2021-05-27", "health_inst_name" => "THQHSB", "delivery_institution" => "32HI084", "total_delivery" => 0 ) ,
    "2" => (object) array ( "dd_id" => 619, "delivery_date" => "2021-05-27", "health_inst_name" => "GHK", "delivery_institution" => "32HI087", "total_delivery" => 3 ) ,
    "3" => (object) array ( "dd_id" => 623, "delivery_date" => "2021-05-27", "health_inst_name" => "CHCM", "delivery_institution" => "32HI085", "total_delivery" => 0 ) ,
    "4" => (object) array ( "dd_id" => 625, "delivery_date" => "2021-05-27", "health_inst_name" => "THQHV", "delivery_institution" => "32HI086", "total_delivery" => 1 ) ,
    "5" => (object) array ( "dd_id" => 523, "delivery_date" => "2021-05-26", "health_inst_name" => "THQHSB", "delivery_institution" => "32HI084", "total_delivery" => 4 ) ,
    "6" => (object) array ( "dd_id" => 522, "delivery_date" => "2021-05-26", "health_inst_name" => "CHCM", "delivery_institution" => "32HI085", "total_delivery" => 1 ) ,
    "7" => (object) array ( "dd_id" => 521, "delivery_date" => "2021-05-26", "health_inst_name" => "THQHV", "delivery_institution" => "32HI086", "total_delivery" => 0 ) ,
    "8" => (object) array ( "dd_id" => 520, "delivery_date" => "2021-05-26", "health_inst_name" => "GHK", "delivery_institution" => "32HI087", "total_delivery" => 1 ) ,
    "9" => (object) array ( "dd_id" => 518, "delivery_date" => "2021-05-26", "health_inst_name" => "DHM", "delivery_institution" => "32HI083", "total_delivery" => 7 ) ,
    "10" => (object) array ( "dd_id" => 425, "delivery_date" => "2021-05-25", "health_inst_name" => "THQHV", "delivery_institution" => "32HI086", "total_delivery" => 1 ) ,
    "11" => (object) array ( "dd_id" => 424, "delivery_date" => "2021-05-25", "health_inst_name" => "CHCM", "delivery_institution" => "32HI085", "total_delivery" => 0 ) ,
    "12" => (object) array ( "dd_id" => 422, "delivery_date" => "2021-05-25", "health_inst_name" => "DHM", "delivery_institution" => "32HI083", "total_delivery" => 7 ) ,
    "13" => (object) array ( "dd_id" => 419, "delivery_date" => "2021-05-25", "health_inst_name" => "THQHSB", "delivery_institution" => "32HI084", "total_delivery" => 2 ) ,
    "14" => (object) array ( "dd_id" => 418, "delivery_date" => "2021-05-25", "health_inst_name" => "GHK", "delivery_institution" => "32HI087", "total_delivery" => 2 ) ,
    "15" => (object) array ( "dd_id" => 336, "delivery_date" => "2021-05-24", "health_inst_name" => "GHK", "delivery_institution" => "32HI087", "total_delivery" => 5 ) ,
    "16" => (object) array ( "dd_id" => 337, "delivery_date" => "2021-05-24", "health_inst_name" => "DHM", "delivery_institution" => "32HI083", "total_delivery" => 6 ) ,
    "17" => (object) array ( "dd_id" => 338, "delivery_date" => "2021-05-24", "health_inst_name" => "CHCM", "delivery_institution" => "32HI085", "total_delivery" => 0 ) ,
    "18" => (object) array ( "dd_id" => 340, "delivery_date" => "2021-05-24", "health_inst_name" => "THQHV", "delivery_institution" => "32HI086", "total_delivery" => 0 ) ,
    "19" => (object) array ( "dd_id" => 339, "delivery_date" => "2021-05-24", "health_inst_name" => "THQHSB", "delivery_institution" => "32HI084", "total_delivery" => 3 ) ,
    "20" => (object) array ( "dd_id" => 246, "delivery_date" => "2021-05-23", "health_inst_name" => "GHK", "delivery_institution" => "32HI087", "total_delivery" => 0 ) ,
    "21" => (object) array ( "dd_id" => 255, "delivery_date" => "2021-05-23", "health_inst_name" => "THQHV", "delivery_institution" => "32HI086", "total_delivery" => 1 ) ,
    "22" => (object) array ( "dd_id" => 253, "delivery_date" => "2021-05-23", "health_inst_name" => "THQHSB",  "delivery_institution" => "32HI084", "total_delivery" => 4 ) ,
    "23" => (object) array ( "dd_id" => 251, "delivery_date" => "2021-05-23", "health_inst_name" => "DHM", "delivery_institution" => "32HI083", "total_delivery" => 4 ) ,
    "24" => (object) array ( "dd_id" => 249, "delivery_date" => "2021-05-23", "health_inst_name" => "CHCM", "delivery_institution" => "32HI085", "total_delivery" => 0 ) ,
    "25" => (object) array ( "dd_id" => 197, "delivery_date" => "2021-05-22", "health_inst_name" => "DHM", "delivery_institution" => "32HI083", "total_delivery" => 10 ) ,
    "26" => (object) array ( "dd_id" => 196, "delivery_date" => "2021-05-22", "health_inst_name" => "THQHV", "delivery_institution" => "32HI086", "total_delivery" => 0 ) ,
    "27" => (object) array ( "dd_id" => 193, "delivery_date" => "2021-05-22", "health_inst_name" => "THQHSB", "delivery_institution" => "32HI084", "total_delivery" => 2 ) ,
    "28" => (object) array ( "dd_id" => 194, "delivery_date" => "2021-05-22", "health_inst_name" => "GHK", "delivery_institution" => "32HI087", "total_delivery" => 1 ) ,
    "29" => (object) array ( "dd_id" => 195, "delivery_date" => "2021-05-22", "health_inst_name" => "CHCM", "delivery_institution" => "32HI085", "total_delivery" => 1 ),
     );
    
    $twodim=[];
    foreach ($array2 as $n => $data) {
        if (!isset($twodim[$data->delivery_date])) $twodim[$data->delivery_date] = array();
        if (!isset($twodim[$data->delivery_date][$data->health_inst_name])) $twodim[$data->delivery_date][$data->health_inst_name] = 0;
        $twodim[$data->delivery_date][$data->health_inst_name] += intVal($data->total_delivery) ;
    
    }
    
    print_r($twodim);
    

    这将为您提供以下输出,您可以轻松地将其操作到您在问题中说明的表格中:

    Array
    (
        [2021-05-28] => Array
            (
                [DHM] => 8
            )
    
        [2021-05-27] => Array
            (
                [THQHSB] => 0
                [GHK] => 3
                [CHCM] => 0
                [THQHV] => 1
            )
    
        [2021-05-26] => Array
            (
                [THQHSB] => 4
                [CHCM] => 1
                [THQHV] => 0
                [GHK] => 1
                [DHM] => 7
            )
    
        [2021-05-25] => Array
            (
                [THQHV] => 1
                [CHCM] => 0
                [DHM] => 7
                [THQHSB] => 2
                [GHK] => 2
            )
    
        [2021-05-24] => Array
            (
                [GHK] => 5
                [DHM] => 6
                [CHCM] => 0
                [THQHV] => 0
                [THQHSB] => 3
            )
    
        [2021-05-23] => Array
            (
                [GHK] => 0
                [THQHV] => 1
                [THQHSB] => 4
                [DHM] => 4
                [CHCM] => 0
            )
    
        [2021-05-22] => Array
            (
                [DHM] => 10
                [THQHV] => 0
                [THQHSB] => 2
                [GHK] => 1
                [CHCM] => 1
            )
    
    )
    

    【讨论】:

    • 第一个!isset() 是不必要的。在 php.ini 中,父级别不需要在子级别之前声明。如果这项工作是如何获得数据的运行计数,我们已经在 Stack Overflow 上有重复的工作。
    猜你喜欢
    • 1970-01-01
    • 2012-11-29
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多