简体   繁体   English

动态显示多维数组

[英]Display multidimensional array dynamically

I have this array, 我有这个数组

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

   ) 

and I want to display it dynamically depending on the array values. 我想根据数组值动态显示它。

Sample format: 样本格式:

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

and so on… 等等…

I'm stuck in this part. 我被困在这一部分。

Here's my code 这是我的代码

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

I want the val to be dynamic. 我希望val是动态的。 I don't know how to count the number of values return in an array. 我不知道如何计算数组中返回的值的数量。

You an run the foreach loop inside for loop. 您在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>";
        }

You can nicely format your data in table like code bellow, but you have to make sure all inner arrays have the same length 您可以像下面的代码一样很好地格式化表中的数据,但是必须确保所有内部数组的长度都相同

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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM