PHP 5.6展平具有多个字段的多维数组

时间:2018-06-01 08:38:49

标签: php arrays

我有一个多维数组,如下所示:

array (size=2)
  0 => 
    array (size=3)
      'a1' => boolean true
      'b1' => string 'abc' (length=6)
      'c1' => string 'def' (length=8)
  1 => 
    array (size=5)
      'a2' => boolean true
      'b2' => string 'fgh' (length=6)
      'c2' => string 'ijk' (length=8)
      'd2' => string 'lmn' (length=4)
      'e2' => string 'opq' (length=4)

我的目标是得到这个:

array (size=8)
  'a1' => boolean true
  'b1' => string 'abc' (length=6)
  'c1' => string 'def' (length=8)
  'a2' => boolean true
  'b2' => string 'fgh' (length=6)
  'c2' => string 'ijk' (length=8)
  'd2' => string 'lmn' (length=4)
  'e2' => string 'opq' (length=4)

我尝试过使用array_map函数,但我不知道如何获取所有字段:

$params = array_map(function($element) {
    return $element['a1'];
}, $params);

8 个答案:

答案 0 :(得分:2)

使用call_user_func_array()之类的:

  $arr1=array ( 
        array (
          'a1' => true,
          'b1' => 'abc',
          'c1' => 'def'), 
        array (
          'a2' => true,
          'b2' => 'fgh',
          'c2' => 'ijk',
          'd2' => 'lmn',
          'e2' => 'opq'));
        $res=call_user_func_array('array_merge', $arr1);
    print_r($res);

输出:

Array ( [a1] => 1 [b1] => abc [c1] => def [a2] => 1 [b2] => fgh [c2] => ijk 
[d2] => lmn [e2] => opq )

了解call_user_func_array

答案 1 :(得分:0)

尝试使用它:

function array_flatten($array) {
    if (!is_array($array)) {
        return FALSE;
    }
    $result = array();
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $result = array_merge($result, array_flatten($value));
        }
        else {
            $result[$key] = $value;
        }
    }
    return $result;
}

我用以下方法测试:

$array = array(
    0 => array(
        'a1' => true,
        'b1' => 'abc',
        'c1' => 'def',
    ),
    1 => array(
        'a2' => true,
        'b2' => 'fgh',
        'c2' => 'ijk',
        'd2' => 'lmn',
        'e2' => 'opq'
    )
);

$result = array_flatten($array);

var_dump($result);

输出结果为:

array (size=8)
    'a1' => boolean true
    'b1' => string 'abc' (length=3)
    'c1' => string 'def' (length=3)
    'a2' => boolean true
    'b2' => string 'fgh' (length=3)
    'c2' => string 'ijk' (length=3)
    'd2' => string 'lmn' (length=3)
    'e2' => string 'opq' (length=3)

但我更愿意做到这一点,我认为这是我从一个重复的问题得到的功能,再也找不到了!但是我用它来“平坦化”#34;一些数组,它运作良好:))

答案 2 :(得分:0)

您需要使用循环来合并数组

<?php
$arr = [
  ["a1"=>true,"b1"=>"abc"],
  ["a2"=>true,"b2"=>"fgh"]
];
$new = [];
foreach($arr as $key=>$val){
  $new = array_merge($new,$val);
}
print_r($new);
?>

Live Demo

...array_values,关于...

<?php
$arr = [
  ["a1"=>true,"b1"=>"abc"],
  ["a2"=>true,"b2"=>"fgh"]
];
$arr = array_merge(...array_values($arr));
print_r($arr);
?>

Live Demo

答案 3 :(得分:0)

对于示例中的数据,您可以使用array_reduce,传递并清空数组作为初始值并添加数组:

$arrays = [
    [
        "a1" => true,
        "b1" => "abc",
        "c1" => "def"
    ],
    [
        "a2" => true,
        "b2" => "fgh",
        "c2" => "ijk",
        "d2" => "lmn",
        "e2" => "opq"
    ]
];

$result = array_reduce($arrays, function ($carry, $item) {
        $carry += $item;
        return $carry;
}, []);

var_dump($result);

Demo

这将导致:

array(8) {
  ["a1"]=>
  bool(true)
  ["b1"]=>
  string(3) "abc"
  ["c1"]=>
  string(3) "def"
  ["a2"]=>
  bool(true)
  ["b2"]=>
  string(3) "fgh"
  ["c2"]=>
  string(3) "ijk"
  ["d2"]=>
  string(3) "lmn"
  ["e2"]=>
  string(3) "opq"
}

答案 4 :(得分:0)

以下是解决方案:

$d = array (
array (
'a1' => true,
'b1' => 'abc' ,
'c1' => 'def' ),
array (
'a2' => true,
'b2' => 'fgh',
'c2' => 'ijk' ,
'd2' => 'lmn' ,
'e2' => 'opq' ));

function flatten(array $array)
{
    $result = array();
    if (is_array($array)) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                $result = array_merge($result, flatten($v));
            } else {
                $result[$k] = $v;
            }
        }
    }
    return $result;
}

$dd = flatten($d);
echo "<pre>";
print_r($dd);
echo "</pre>";

DEMO:https://3v4l.org/jtUoq

答案 5 :(得分:0)

使用<tr> <td><p style="user-select: none;">test</p></td> <td><input type="button" disabled="disabled"></td> </tr> 。来自documentation

  

可用于迭代递归迭代器。

示例:

RecursiveIteratorIterator

演示:https://3v4l.org/QX9iQ

答案 6 :(得分:0)

$flatArray = array(); // an empty array where we will store rest of arrays

foreach($multiArray as $item){
$flatArray = array_merge($flatArray,$item); //get arrays and merge into main array
}

答案 7 :(得分:0)

到达foreach:

<?php

$data =
[
    [
        'foo' => true,
        'bar' => false
    ],
    [
        'baz' => true,
        'bat' => true
    ]
];

$out = [];
foreach($data as $item)
    foreach($item as $k => $v)
        $out[$k] = $v;

var_export($out);

输出:

array (
'foo' => true,
'bar' => false,
'baz' => true,
'bat' => true,
)