使用PHP进行数组迭代和合并


Array iteration and merging using PHP

我有一个像这样的数组:'

<>之前的数组([0] =>数组([0] =>数组([productid] => 141[fieldid] => 2[value] =>笔记本电脑)[1] =>数组([productid] => 191[fieldid] => 2[value] =>图书)[2] =>数组([productid] => 177[fieldid] => 2[value] =>打印机))[1] =>数组([0] =>数组([productid] => 141[fieldid] => 1[value] => 3)[1] =>数组([productid] => 191[fieldid] => 1[value] => 4)[2] =>数组([productid] => 177[fieldid] => 1[value] => 2))) 之前

我想修改这个数组,看起来像这样:

<>之前数组([0] =>数组([productid] => 141[fieldid] => 2[value] =>笔记本电脑)[1] =>数组([productid] => 191[fieldid] => 2[value] =>图书)[2] =>数组([productid] => 177[fieldid] => 2[value] =>打印机)[3] =>阵列([productid] => 141[fieldid] => 1[value] => 3)[4] =>数组([productid] => 191[fieldid] => 1[value] => 4)[5] =>数组([productid] => 177[fieldid] => 1[value] => 2))之前

只需删除外部数组并将数组的所有块合并为一个。这在php中可能吗?

一种方法是:

假设$items是原始数组

$new_items = array();
foreach($items as $item)
{
    $new_items = array_merge($new_items,array_values($item));
}

是的,您可以使用以下方法来完成此操作。很简单。

//let the older array be $array
var $newArray = array();  //new array(all itms will be taken to this array)      
foreach($array as $key->$value){
    foreach($value as $key->$innervalue){
            $newArray[] = $innervalue;
             }
    }