动态显示多维数组

时间:2015-06-13 03:36:05

标签: php arrays

我有这个数组,

Array
(
[part_number] => Array
    (
        [1] => "88888"
        [2] => "898989"
        [3] => "12312"
        [4] => "321321321"
    )

[manufacturer] => Array
    (
        [1] => "Dell"
        [2] => "Toshiba"
        [3] => "Asus"
        [4] => "AMD"
    )

[description] => Array
    (
        [1] => "i3 Processor"
        [2] => "i5 Processor"
        [3] => "i7 Processor"
        [4] => "Video Card 4GB"
    )

[list_price] => Array
    (
        [1] => "450"
        [2] => "100"
        [3] => "100"
        [4] => "150"
    )

[net_price] => Array
    (
        [1] => "500"
        [2] => "120"
        [3] => "120"
        [4] => "200"
    )

[quantity] => Array
    (
        [1] => "600"
        [2] => "150"
        [3] => "150"
        [4] => "80"
    )

[measure] => Array
    (
        [1] => "14 inch"
        [2] => "Pc/s"
        [3] => "Pc/s"
        [4] => "Pc/s"
       )

   ) 

我希望根据数组值动态显示它。

样本格式:

part_number : "88888"
manufacturer : "Dell"
description : "i3 Processor"
list_price : "450"
net_price : "500"
quantity : "600"
measure : "14 inch"

依旧......

我被困在这一部分。

这是我的代码

<?php foreach($success_arr as $success_part_number => $val): ?>
    <?=$success_part_number." : ".$val[1];?>
<?php endforeach; ?>

我希望val是动态的。我不知道如何计算数组中返回值的数量。

2 个答案:

答案 0 :(得分:0)

你在for循环中运行foreach循环。

   $counter = count($array['manufacturer']);
        for ($i=0; $i < $counter; $i++) 
        { 
            echo "<ul>";
            foreach($array as $success_part_number => $val)
            {
                echo "<li>";
                echo $success_part_number." : ".$val[$i];
                echo "</li>";
            }
            echo "</ul>";
            echo "<hr>";
        }

答案 1 :(得分:0)

你可以很好地格式化表格中的数据,如下面的代码,但你必须确保所有内部数组具有相同的长度

echo '<table>';
// $i is your starting point - 1;
for($i = 0; $i<= count($arr['part_number']); $i++)
{
    foreach($arr as $k => $v)
    {
    // with first loop we build table headers => your inner array keys
        if($i==0) {
            echo '<th>' .$k. '</th>';
            continue;
        } 
        // start to build your data rows
        echo '<td>' . $v[$i] . '</td>';
    }
    echo '</tr>';
}
echo '</table>';