-1

I have two arrays like this:

$a1 = array(
    array('fisrtname' => 'john', 'lastname' => 'smith', 'c1' => 10),
    array('fisrtname' => 'jane', 'lastname' => 'smith', 'c1' => 20),
    ...
);

$a2 =  = array(
    array('fisrtname' => 'john', 'lastname' => 'smith', 'c2' => 40),
    array('fisrtname' => 'jane', 'lastname' => 'smith', 'c2' => 50),
    ...
);

I need to combine these two like this:

$a3 =  = array(
    array('fisrtname' => 'john', 'lastname' => 'smith', 'c1' => 10, 'c2' => 40),
    array('fisrtname' => 'jane', 'lastname' => 'smith', 'c1' => 20, 'c2' => 50),
    ...
);

How can I do it using PHP and less code?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
nexita
  • 35
  • 1
  • 6
  • @Rizier123: I don't want to put lines of simple codes to make it done. Actually I'm looking for using some ready functions together, but I'd no idea. – nexita Feb 26 '15 at 14:51
  • This question contains an insufficient [mcve]. As a result, you have received answers which provide the desired result by happenstance. A better sample set of data would clarify if the two arrays may have a different number of rows or if some rows from one array might not have a corresponding row in the opposite array or if the related rows do not share a first level index. We are left to assume that `fisrtname` and `lastname` must be used to determine the relationship between arrays. – mickmackusa Sep 12 '22 at 08:08
  • Exact duplicate of [Merge row data from multiple arrays](https://stackoverflow.com/q/16541555/2943403) – mickmackusa Oct 10 '22 at 22:53

3 Answers3

1

The function you're looking for is probably just a single function call: array_replace_recursive()

$a1 = array(
    array('fisrtname' => 'john', 'lastname' => 'smith', 'c1' => 10),
    array('fisrtname' => 'jane', 'lastname' => 'smith', 'c1' => 20),
);

$a2 =  array(
    array('fisrtname' => 'john', 'lastname' => 'smith', 'c2' => 40),
    array('fisrtname' => 'jane', 'lastname' => 'smith', 'c2' => 50),
);

$result = array_replace_recursive($a1, $a2);
var_dump($result);

which gives:

array(2) {
  [0]=>
  array(4) {
    ["fisrtname"]=>
    string(4) "john"
    ["lastname"]=>
    string(5) "smith"
    ["c1"]=>
    int(10)
    ["c2"]=>
    int(40)
  }
  [1]=>
  array(4) {
    ["fisrtname"]=>
    string(4) "jane"
    ["lastname"]=>
    string(5) "smith"
    ["c1"]=>
    int(20)
    ["c2"]=>
    int(50)
  }
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Please clarify that your answer does not "put other elements of two array beside similar ones", but simply merges arrays based on their first level index. Additionally, you may wish to vote to close as Unclear because of the low-quality task description and lackluster mcve. – mickmackusa Sep 12 '22 at 08:13
0

You can use array_map and array_merge functions to accomplish this.

$a3 = array_map("array_merge",$a1, $a2);

Vivek Vaghela
  • 1,075
  • 9
  • 16
  • Please clarify that your answer does not "put other elements of two array beside similar ones", but simply merges arrays based on their first level index. Additionally, you may wish to vote to close as Unclear because of the low-quality task description and lackluster mcve. – mickmackusa Sep 12 '22 at 08:13
0

If the array a1 and a2 length is same, You can use code below:

<?php

$a1 = array(
    array('fisrtname' => 'john', 'lastname' => 'smith', 'c1' => 10),
    array('fisrtname' => 'jane', 'lastname' => 'smith', 'c1' => 20)
);

$a2 = array(
    array('fisrtname' => 'john', 'lastname' => 'smith', 'c2' => 40),
    array('fisrtname' => 'jane', 'lastname' => 'smith', 'c2' => 50)
);

for ($i=0; $i < count($a1); $i++) { 
    $a3[$i] = array_merge($a1[$i],$a2[$i]);
    $a3[$i] = array_unique($a3[$i]);
}

var_dump($a3);

?>
  • Please clarify that your answer does not "put other elements of two array beside similar ones", but simply merges arrays based on their first level index. Additionally, you may wish to vote to close as Unclear because of the low-quality task description and lackluster mcve. – mickmackusa Sep 12 '22 at 08:13