来自多维数组的数组合并

时间:2019-02-25 07:35:39

标签: php arrays laravel

我想将多维数组合并为单个数组。 我想知道是否有php built in array_function可以做到这一点。我不想使用任何循环,因为我有导入文件中的数据。可能有数千条记录。

我在下面的代码下生成此数组:

$insert = array();
foreach ($data as $key => $value) {
   $insert[$key][] = [
        'name' => $value->name,
        'email' => $value->email,
        'mobile_no' => $value->mobile_no,
        'password' => bcrypt($value->password),
        'user_type_id' => $value->user_type_id,
        'designation' => $value->designation,

        $this->userTypeWiseFieldGenerateForImportInsert($value)
    ];
}

我的回调函数是

public function userTypeWiseFieldGenerateForImportInsert($data){
    if($data->user_type_id == 1){
        $insertArray['market_code'] = $data->market_code;
        $insertArray['product_code'] = $data->product_code;
        $insertArray['territori_code'] = $data->territori_code;
        $insertArray['region_code'] = $data->region_code;
        $insertArray['division_code'] = $data->division_code;
    }elseif($data->user_type_id == 2){
        $insertArray['territori_code'] = $data->territori_code;
        $insertArray['region_code'] = $data->region_code;
        $insertArray['division_code'] = $data->division_code;
    }elseif($data->user_type_id == 3){
        $insertArray['region_code'] = $data->region_code;
        $insertArray['division_code'] = $data->division_code;
    }elseif($data->user_type_id == 4){
        $insertArray['division_code'] = $data->division_code;
    }

    return $insertArray;
}

我得到了这个数组:

Array(
  [0] => Array
      (
          [name] => Md. XXXX
          [email] => abc@gmail.com
          [mobile_no] => 1751017812
          [password] => $2y$10$6Vmr61t896IxfJ1A0pxX5Of1hnfUVX9blmdmclAxt56cMd754NkDC
          [user_type_id] => 1
          [designation] => Sr. Software Engineer
          [0] => Array
              (
                  [market_code] => mirpurA203
                  [product_code] => Seclo201
                  [territori_code] => T352
                  [region_code] => Mirpur334
                  [division_code] => Dhaka31
              )

      )

  [1] => Array
      (
          [name] => Md. XX
          [email] => def@gmail.com
          [mobile_no] => 1761017812
          [password] => $2y$10$62fwqiAmrv.Jc89i5L3YNuMpcYeFVrpqewuBPKYIgmXiX/9sGRC/S
          [user_type_id] => 2
          [designation] => Sr. Software Engineer
          [0] => Array
              (
                  [territori_code] => T352
                  [region_code] => Mirpur334
                  [division_code] => Dhaka31
              )

      )

) 

我希望制作单个数组,如:

Array(
[0] => Array
    (
        [name] => Md. XXXX
        [email] => abc@gmail.com
        [mobile_no] => 1751017812
        [password] => $2y$10$6Vmr61t896IxfJ1A0pxX5Of1hnfUVX9blmdmclAxt56cMd754NkDC
        [user_type_id] => 1
        [designation] => Sr. Software Engineer
        [market_code] => mirpurA203
        [product_code] => Seclo201
        [territori_code] => T352
        [region_code] => Mirpur334
        [division_code] => Dhaka31

    )

[1] => Array
    (
        [name] => Md. XX
        [email] => def@gmail.com
        [mobile_no] => 1761017812
        [password] => $2y$10$62fwqiAmrv.Jc89i5L3YNuMpcYeFVrpqewuBPKYIgmXiX/9sGRC/S
        [user_type_id] => 2
        [designation] => Sr. Software Engineer
        [territori_code] => T352
        [region_code] => Mirpur334
        [division_code] => Dhaka31

    )

)

6 个答案:

答案 0 :(得分:2)

类似这样的方法可以解决问题:

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

$arr = ['one','two', 'three', ['four','five']];   
print_r(test($arr));
// Output: Array ( [0] => one [1] => two [2] => three [3] => four [4] => five )

希望这会有所帮助,

答案 1 :(得分:2)

要修复您的原始导入...

您的子函数(userTypeWiseFieldGenerateForImportInsert())首先将子数组添加到数据中,您可以从初始数据中创建一个部分,然后合并此函数调用中的其他数据。 ..

foreach ($data as $key => $value) {
    $newData = [
        'name' => $value->name,
        'email' => $value->email,
        'mobile_no' => $value->mobile_no,
        'password' => bcrypt($value->password),
        'user_type_id' => $value->user_type_id,
        'designation' => $value->designation,
    ];
    $additionalData = $this->userTypeWiseFieldGenerateForImportInsert($value);

    $insert[$key][] = array_merge($newData, $additionalData);
}

答案 2 :(得分:1)

签出array_walk_recursive()功能。例如:

$array = /*your array example*/;
$filter = [];
foreach ($array as $index => $item) {
  array_walk_recursive($item, function($val, $key) use (&$filter) {
      if (strpos($key, 'territori_code') === 0) {
        $filter[$key] = $val;
      }
  });
}

注意:确定保留哪些密钥的标准是什么?
我在以下地方有参考文献:https://codereview.stackexchange.com/questions/45281/merge-some-child-values-back-into-the-parent-multidimensional-array

答案 3 :(得分:1)

没有内置的PHP函数可以帮助您轻松实现目标,您必须执行循环或递归函数。

答案 4 :(得分:1)

可以通过使用array_map().

创建一个简单函数来解决

在下面的代码中,merge_my_array()函数会将多维数组合并为单个数组,而不管嵌套数组如何包含它。

$arr = array(
    0 => array(
            'name' => 'Md. 1111',
            'email' => 'abc@gmail.com',
            'mobile_no' => '111111',
            'password' => '$2y$10$6Vmr61t896IxfJ1A0pxX5Of1hnfUVX9blmdmclAxt56cMd754NkDC',
            'user_type_id' => 1,
            'designation' => 'Sr. Software Engineer',
            0 => array(
                    'market_code' => 'mirpurA203',
                    'product_code' => 'Seclo201',
                    'territori_code' => 'T352',
                    'region_code' => 'Mirpur334',
                    'division_code' => 'Dhaka31',
              )
        ),
    1 => array(
      'name' => 'Md. 222',
      'email' => 'def@gmail.com',
      'mobile_no' => '222222',
      'password' => '$2y$10$62fwqiAmrv.Jc89i5L3YNuMpcYeFVrpqewuBPKYIgmXiX/9sGRC/S',
      'user_type_id' => 2,
      'designation' => 'Sr. Software Engineer',
      0 => array
          (
              'territori_code' =>'T352',
              'region_code' => 'Mirpur334',
              'division_code' => 'Dhaka31',
          )

    )
);

$out = array();
array_map("merge_my_array",$arr);

echo "<pre> out: "; print_r( $out );echo "</pre>";




function merge_my_array( $elem ){
    global $out;
    if( is_array($elem) ){
        array_map( "merge_my_array", $elem );
    }else{
        array_push($out,$elem);
        return;
    }
}

答案 5 :(得分:1)

您也可以在没有for循环的情况下解决合并数组。

您可以尝试遵循以下代码吗?

function reduce(&$item, $key){
    $item = array_merge($item, $item[0]);
    unset($item[0]);
}       

array_walk($data, 'reduce');
var_dump($array);