遍历数组数组 PHP

Loop throuh array of arrays PHP

我有一个数组,它是从我的 $_POST 数据中得到的

Array ( 
    [unit] => 
    [items] => Array ( 
        [1] => Array ( 
            [category] => 1 
            [items] => 5 
            [qty] => 1 
            [jan] => 1 
            [feb] => 
            [mar] => 
            [apr] => 
            [may] =>
            [jun] => 
            [jul] => 
            [aug] => 
            [sep] => 
            [oct] => 
            [nov] => 
            [dec] => 
        ) 
        [2] => Array ( 
            [category] => 1 
            [items] => 20 
            [qty] => 1 
            [jan] => 1 
            [feb] => 
            [mar] => 
            [apr] => 
            [may] => 
            [jun] => 
            [jul] => 
            [aug] => 
            [sep] => 
            [oct] => 
            [nov] => 
            [dec] => 
        ) 
        [3] => Array ( 
            [category] => 1 
            [items] => 27 
            [qty] => 1 
            [jan] => 1 
            [feb] => 
            [mar] => 
            [apr] => 
            [may] => 
            [jun] => 
            [jul] => 
            [aug] => 
            [sep] => 
            [oct] => 
            [nov] => 
            [dec] => 
        ) 
    ) 
    [action] => 
)

我正在尝试获取每个数组并将其传递到我的模型以进行数据库插入。例如获取 [items] 的数组,其中 [1] 点。

我试过使用

$array_col = array_column($_POST, 'items');
print_r($array_col);

但是它 returns Array() 是空的。

感谢您的回答。

我怀疑你的数据在$_POST['items']中。 所以:

$array_col = $_POST['items'];

然后,要遍历它们,您需要 loop.

foreach($array_col as $col){
  // Do your stuff here
  print_r($col);
}

来自 PHP 文档 ...
http://php.net/manual/en/function.array-column.php
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array

检查您是否有数组键索引 -“items”处于不同级别(0 级和 2 级)

您可以使用此代码。

foreach($_POST['items'] as $a){
    // Code will go here. Whatever.
    print_r($a);
}