2

I have two result set from database first one is :

Array(
    [0] => Array([vehicle] => BUS NO.1 )
    [1] => Array([vehicle] => BUS NO.2)}

second one is :

Array([0] => Array(
            [trip_name] => Trip00011
            [running_km] => 5000)
      [1] => Array(
            [trip_name] => Trip00021
            [running_km] => 2400))

I want to merge these arrays like following array:

Array([0] => Array(
            [vehicle] => BUS NO.1
            [trip_name] => Trip00011
            [running_km] => 5000 )
        [1] => Array(
            [vehicle] => BUS NO.2
            [trip_name] => Trip00011
            [running_km] => 5000)
        [2] => Array(
            [vehicle] => BUS NO.1
            [trip_name] => Trip00021
            [running_km] => 2400 )
        [3] => Array(
            [vehicle] => BUS NO.2
            [trip_name] => Trip00021
            [running_km] => 2400))

Actually, this is my question.Could you please help me to do this

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
Miya
  • 33
  • 7
  • 4
    Have you looked at the [array_merge](http://php.net/manual/en/function.array-merge.php) page of the PHP documentation? – Peppermintology Sep 14 '18 at 09:48
  • yes, but this is not two arrays. looped arrays of one array. – Miya Sep 14 '18 at 09:52
  • `this is not two arrays. looped arrays of one array` - Well thats not quite the question you asked, perhaps you could elaborate in your question. – Peppermintology Sep 14 '18 at 09:56
  • If you just want to add all your result from 2 array in one array in not particular order : just create a new array, one foreach on first array and add result on result array, then foreach on second array and add result on result array...did you try something? – Mickaël Leger Sep 14 '18 at 09:56
  • Just use the `+` operator to add your two Arrays...Or array_merge as said above... – Elementary Sep 14 '18 at 10:08
  • @Elementary it's better to use `array_merge` than `+`, because plus operator will skip duplicated keys – Agnius Vasiliauskas Sep 14 '18 at 10:43
  • he has edited the question since my comment @AgniusVasiliauskas – Elementary Sep 14 '18 at 11:01

2 Answers2

1

Given these following arrays

$array1=Array(
            0 => Array('vehicle' => 'BUS NO.1' ),
            1 => Array('vehicle' => 'BUS NO.2')
        );

$array2=Array(
            0 => Array(
                'trip_name' => 'Trip00011',
                'running_km' => 5000),
            1 => Array(
                'trip_name' => 'Trip00021',
                'running_km' => 2400)
        );

You can obtain the expected output using either the simplest way

foreach($array2 as $val){
        foreach($array1 as  $v){
             $result[]=$v+$val;
        }
    }

or the complex(maybe the worst) way:

!($result=[])?array_map(function($a) use ($array1,&$result){ $result=array_merge($result,array_map(function($val)use($a){ return $val+$a;},$array1));},$array2):[];

ouput:

array(4) {
  [0]=>
  array(3) {
    ["vehicle"]=>
    string(8) "BUS NO.1"
    ["trip_name"]=>
    string(9) "Trip00011"
    ["running_km"]=>
    int(5000)
  }
  [1]=>
  array(3) {
    ["vehicle"]=>
    string(8) "BUS NO.2"
    ["trip_name"]=>
    string(9) "Trip00011"
    ["running_km"]=>
    int(5000)
  }
  [2]=>
  array(3) {
    ["vehicle"]=>
    string(8) "BUS NO.1"
    ["trip_name"]=>
    string(9) "Trip00021"
    ["running_km"]=>
    int(2400)
  }
  [3]=>
  array(3) {
    ["vehicle"]=>
    string(8) "BUS NO.2"
    ["trip_name"]=>
    string(9) "Trip00021"
    ["running_km"]=>
    int(2400)
  }
}
Elementary
  • 1,443
  • 1
  • 7
  • 17
-1

you can use array_merge. array_merge(array1, array2)

parpar
  • 329
  • 1
  • 10