如何用PHP回显多维数组中的每个值?

时间:2016-02-05 16:43:26

标签: php arrays

如何做到这一点?

我希望在多维数组中回显每个值。

这是我的数组的print_r。

Array ( [7] => Array ( [sale] => 08 [not_sale] => 00 [inventory] => -- [not_inventory] => -- ) [1] => Array ( [inventory] => 17 [not_inventory] => 00 [sale] => 08 [not_sale] => 00 ) [2] => Array ( [inventory] => 17 [not_inventory] => 00 [sale] => 08 [not_sale] => 00 ) [3] => Array ( [inventory] => 17 [not_inventory] => 00 [sale] => 08 [not_sale] => 00 ) [4] => Array ( [inventory] => 17 [not_inventory] => 00 [sale] => 08 [not_sale] => 00 ) [5] => Array ( [inventory] => 17 [not_inventory] => 00 [sale] => -- [not_sale] => -- ) [6] => Array ( [inventory] => -- [not_inventory] => -- [sale] => -- [not_sale] => -- ) ) 

我想回应每个数组中的每个项目......

这是我的代码。但没有运气!请尊重

for($row=0; $row<7; $row++) {
    for($col=0; $col<7; $col++) {
        echo $myArray[$row][$col] . "<br/>";
    }
}

3 个答案:

答案 0 :(得分:1)

使用foreach

foreach($myArray as $subArray) {
    foreach($subArray as $key => $val) {
        echo $key . " - " . $val . "<br/>";
    }
}

答案 1 :(得分:0)

<?php
foreach ($arrRows as $arrSingleRow)
{
    echo "Sale         : " . $arrSingleRow["sale"];
    echo "Not Sale     : " . $arrSingleRow["not_sale"];
    echo "Inventory    : " . $arrSingleRow["inventory"];
    echo"<br/>==========================<br/>";
}

答案 2 :(得分:0)

递归函数应该实现您之后的功能。如果您纯粹在一系列价值观之后(关键不重要)尝试类似......

function list_array_values_recursive($array) {
    foreach ($array as $value) {
        if (is_array($value)) {
            list_array_values_recursive($value);
        }
        else {
            echo $value;
            echo '<br />';
        }
    }
}

然后将数组放入要回显的值的函数中,

list_array_values_recursive($your_array);