0

I Have three arrays as array1 array2 array3 follows, I want this combine like as array4.

array1
(
[1869] => 1
[1871] => 1
[0] => 2
[1807] => 1
[1875] => 1
[1811] => 1
[1877] => 1
[1878] => 1
[1879] => 1
[1880] => 1
[1886] => 1
[1850] => 2
[1618] => 3
[1679] => 1
)
array2
(
[1619] => 1
[1625] => 1
)

array3
(
[1111] => 1
[2222] => 1
)

Need Output like:

array4
(
 [1869] => 1
 [1871] => 1
 [0] => 2
 [1807] => 1
 [1875] => 1
 [1811] => 1
 [1877] => 1
 [1878] => 1
 [1879] => 1
 [1880] => 1
 [1886] => 1
 [1850] => 2
 [1618] => 3
 [1679] => 1
 [1619] => 1
 [1625] => 1
 [1111] => 1
 [2222] => 1
 )

I want this array1 and array2 combine like array3.Please some one help me.Thanks in advance.

Sanj
  • 141
  • 1
  • 8
  • 5
    did you try `array_combine()` or `array_merge()` **Example: [array-merge.php](http://php.net/manual/en/function.array-merge.php)** – Murad Hasan Jun 29 '17 at 06:46
  • You can merge by this function: http://php.net/manual/en/function.array-merge.php – Mohammadreza Yektamaram Jun 29 '17 at 06:48
  • @frayne I try this but its gives keys like 0,1,2,3..... – Sanj Jun 29 '17 at 06:49
  • Simply add the arrays: `$array4 = $array1 + $array2 + $array1` see: https://stackoverflow.com/a/3292071/4121573 – Adonis Jun 29 '17 at 12:20
  • Possible duplicate of [PHP: merge two arrays while keeping keys instead of reindexing?](https://stackoverflow.com/questions/3292044/php-merge-two-arrays-while-keeping-keys-instead-of-reindexing) – Adonis Jun 29 '17 at 12:20

2 Answers2

4

If each array is on a variable you can do this,

$final=$arr1 + $arr2 + $arr3;

THat way you will not loose the keys as well. Check the manual here.

pinoyCoder
  • 940
  • 7
  • 20
1

Or

$array1 = array('1869' => 1,'1871' => 1,'0' => 2,'1807' => 1,'1875' => 1,'1811' => 1,'1877' => 1,'1878' => 1,'1879' => 1,'1880' => 1,'1886' => 1,'1850' => 2,'1618' => 3,'1679' => 1);
$array2 = array('1619' => 1,'1625' => 1);
$array3 = array('1111' => 1,'2222' => 1);

$newArray = array_replace( $array1,$array2,$array3);

But I think just using the + is preferred.

print_r($newArray);
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38