doujizhong8352
2018-06-01 08:38
浏览 51
已采纳

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

I have a multidimensional array that looks like this:

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)

And my goal is to get this:

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)

I've tried something with the array_map function but I don't know how to get all fields:

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

图片转代码服务由CSDN问答提供 功能建议

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

   array(size = 2)
 0 =>  
 array(size = 3)
'a1'=> 布尔值true 
'b1'=>  string'abc'(length = 6)
'c1'=> 字符串'def'(长度= 8)
 1 =>  
 array(size = 5)
'a2'=> 布尔值true 
'b2'=> 字符串'fgh'(长度= 6)
'c2'=> 字符串'ijk'(长度= 8)
'd2'=>  string'lmn'(length = 4)
'e2'=>  string'opq'(length = 4)
   
 
 

我的目标是得到这个:

  array(  size = 8)
'a1'=> 布尔值true 
'b1'=>  string'abc'(length = 6)
'c1'=> 字符串'def'(长度= 8)
'a2'=> 布尔值true 
'b2'=> 字符串'fgh'(长度= 6)
'c2'=> 字符串'ijk'(长度= 8)
'd2'=>  string'lmn'(length = 4)
'e2'=>  string'opq'(length = 4)
   
 
 

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

  $ params = array_map(function($ element){
 return $ element ['a1']; 
},$ params); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • dongxianji0968 2018-06-01 08:41
    已采纳

    Try to use this :

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

    I test it with :

    $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);
    

    The output is :

    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)
    

    But I prefer to be honnest, I think it's a function I got from a duplicate question, can't find it again ! But I used it to "flatten" some array and it works well :)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doubi1931 2018-06-01 08:43

    You need to use loop to merge array

    <?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

    Or by ...array_values, About ...

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

    Live Demo

    评论
    解决 无用
    打赏 举报
  • duanche8554 2018-06-01 08:47

    For the data in your example, you might use array_reduce, pass and empty array as the initial value and add the arrays:

    $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

    That will result in:

    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"
    }
    
    评论
    解决 无用
    打赏 举报
  • dscqrkvr9562034621 2018-06-01 08:47

    Here is the solution:

    $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

    评论
    解决 无用
    打赏 举报
  • dqxafj6830 2018-06-01 08:49

    Use RecursiveIteratorIterator. From the documentation.

    Can be used to iterate through recursive iterators.

    Example:

    <?php
    
    $arr = [
        [
          'a1' => true,
          'b1' => 'abc',
          'c1' => 'def'
        ],
        [
          'a2' => true,
          'b2' => 'fgh',
          'c2' => 'ijk',
          'd2' => 'lmn',
          'e2' => 'opq',
        ]
    ];
    
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
    
    foreach($iterator as $key => $value) {
        var_dump($key . ': ' . $value);
    }
    

    Demo: https://3v4l.org/QX9iQ

    评论
    解决 无用
    打赏 举报
  • dongliyun3301 2018-06-01 08:50
    $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
    }
    
    评论
    解决 无用
    打赏 举报
  • dttwois6098 2018-06-01 08:55

    Use call_user_func_array() like:

      $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);
    

    Output:

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

    Read about call_user_func_array

    评论
    解决 无用
    打赏 举报
  • du7133 2018-06-02 03:22

    Reach for 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);
    

    Output:

    array (
    'foo' => true,
    'bar' => false,
    'baz' => true,
    'bat' => true,
    )
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题