doucheng5209 2013-07-17 17:05
浏览 149
已采纳

PHP多维数组,以表格形式显示数据

When I echo the array , result is :

$Items = Array( [147] => Array([ItemName] => Array([0]=>White Snack [1]=>Dark Cookie) [ItemCode] => Array([0]=>IT-WS [1]=>IT-DC ) )  [256] => Array([ItemName] => Array([0]=>Soft Sandwiches [1]=>Hard Drinks) [ItemCode] => Array([0]=>IT-SS [1]=>IT-HD )  )) 

Now I need to display the following result in a tabular form , the indexes 147 and 256 , I would pass them from a separate foreach loop so as to get the correct index value.

-----------147---------------

Name ----------------------------------- Code

White Snack --------------------------- WS

Dark Cookie --------------------------- DC

-----------256---------------

Name ----------------------------------- Code

Soft Sandwiches ---------------------- SS

Hard Drinks ---------------------------- HD

Am not able to achieve this. Please help.

    $ItemIndexes = array(147,256);

    foreach($ItemIndexes as $ItemMainIndex)

    {
         //further code here , 
        //here I would send the $ItemMainIndex to the $Items array 
       //and get the corresponding index value and display it accordingly.
    }
  • 写回答

4条回答 默认 最新

  • duannuochi3549 2013-07-17 18:54
    关注

    The structure of your array didn't go well with the structure of your display, so I made a new array with a different structure

    $prods = array();
    foreach ($Items as $key => $group){
        foreach ($group as $attr => $values){
            foreach ($values as $key2 => $value){
                $prods[$key][$key2][$attr] = $value;
            }
        }
    }
    var_dump($prods);
    

    Output:

    Array(
        [147] => Array(
                [0] => Array(
                        [ItemName] => White Snack
                        [ItemCode] => IT-WS
                    )
                [1] => Array(
                        [ItemName] => Dark Cookie
                        [ItemCode] => IT-DC
                    )
            )
        [256] => Array
                [0] => Array(
                        [ItemName] => Soft Sandwiches
                        [ItemCode] => IT-SS
                    )
                [1] => Array(
                        [ItemName] => Hard Drinks
                        [ItemCode] => IT-HD
                    )
            )
    )
    

    With that, I looped through to make a table:

    $table = '<table>';
    foreach ($prods as $key => $group){
        $table .= '<tr><td colspan="2">'.$key.'</td></tr>';
        $table .= '<tr><td>Name</td><td>Code</td></tr>';
        foreach ($group as $key2 => $values){
            $table .= '<tr>';
            foreach ($values as $attr => $value){
                if ($attr == 'ItemCode'){
                    $parts = explode('-', $value);
                    $table .= '<td>'.$parts[1].'</td>';
                } else {
                    $table .= '<td>'.$value.'</td>';
                }
                $prods[$key][$key2][$attr] = $value;
            }
            $table .= '</tr>';
        }
    }
    $table .= '</table>';
    

    With some styling, it can display more or less like you showed. In the array that you posted, there was a parenthesis out of place, so the structure of the original array that I used was:

    $Items = array(147 => array('ItemName' => array(0=>'White Snack',
                                                            1=>'Dark Cookie'),
                                        'ItemCode' => array(0=>'IT-WS',
                                                            1=>'IT-DC')),
                256 => array('ItemName' => array(0=>'Soft Sandwiches',
                                                            1=>'Hard Drinks'),
                                        'ItemCode' => array(0=>'IT-SS',
                                                            1=>'IT-HD')));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了