1

I want to merge the multidimensional array into single array . I want to know is there any php built in array_function to do this. I don't want to use any loop cause I have the data from import file . There could be more than thousands record.

I am generate this array below this code:

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

And my callback functions are

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

I get this array:

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

My expectation to make single array like:

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
    )
)
Cœur
  • 37,241
  • 25
  • 195
  • 267
MD. Jubair Mizan
  • 1,539
  • 1
  • 12
  • 20

6 Answers6

2

Something like this would do the trick:

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 )

Hope this helps,

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
  • Updated, missed setting the indexes – Miroslav Glamuzina Feb 25 '19 at 07:51
  • Thanks for your answer . I know this tricks but i want to know is there any php built in array_function to do this. i don't want to use any loop cause i have the data from import file . There could be more than thousands record – MD. Jubair Mizan Feb 25 '19 at 07:56
  • 2
    @MD.JubairMizan There will be a for loop running somewhere. Built in functions just abstract it but that's what they do under the hood. Nothing wrong with writing your own for loop for it. – Nathan Feb 25 '19 at 08:01
  • @MD.JubairMizan built-in functions perform the same iterations internally, there isn't any magic that they perform. PHP applications are capable of iterating over hundreds of thousands of records quickly, depending on the overall data size. – Will B. Feb 25 '19 at 08:01
  • As @MD.JubairMizan stated, For what you need, this would get the job done. There will be some sort of loop going on somewhere. Also, as far as I am aware, there is not a built-in php function for this. – Miroslav Glamuzina Feb 25 '19 at 08:04
2

To fix your original import...

Your sub-function (userTypeWiseFieldGenerateForImportInsert()) is adding the sub-array into the data in the first place, you could instead create one part from the initial data, and merge in the additional data from this function call...

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);
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
1

Check out the array_walk_recursive() function. For example:

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

Note: What's the criteria for deciding which keys to keep?
I have a reference at: https://codereview.stackexchange.com/questions/45281/merge-some-child-values-back-into-the-parent-multidimensional-array

Dinh Luong
  • 127
  • 3
  • 15
  • Thanks for your answer . I know this tricks but i want to know is there any php built in array_function to do this. i don't want to use any loop cause i have the data from import file . There could be more than thousands record – MD. Jubair Mizan Feb 25 '19 at 07:56
1

There is no PHP builtin function that can help you to achieve your goal easily, you have to go through loop or recursive function.

1

It can be solved by using creating a simple function using array_map().

In the below code, merge_my_array() function will merge the multidimensional array into single, regardless of how may the nested array contains it.

$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;
    }
}
Nishad Up
  • 3,457
  • 1
  • 28
  • 32
1

You can solve merge array also without for loop.

Can you try following codes?

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

array_walk($data, 'reduce');
var_dump($array);
FGDeveloper
  • 1,030
  • 9
  • 23