I have two arrays generating from two sql queries as $array_1 and $array_2. both contain [0] indexes. I want to join them with indexes [0],[1] and so on... So can any one help me on how to concatenate two arrays in php
-
Have you tried `$newArray = array_merge($array_1, $array_2);` or `$newArray = $array_1 + $array_2;` – Mark Baker Apr 10 '17 at 08:18
2 Answers
array_merge — Merge one or more arrays
Description
array array_merge ( array $array1 [, array $... ] )
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
Zipping arrays:
If you want [0]
to be array1[0]
and [1]
to be array2[0]
etc., you can zip the arrays;
I guess you are looking for array_merge
?
https://www.w3schools.com/php/func_array_merge.asp
In your case: array_merge($array_1,$array_2)

- 962
- 10
- 34
-
4Surely better to post a reference to the actual [PHP Docs](http://www.php.net/manual/en/function.array-merge.php), rather than to a third party (with a dubious reputation) like w3schools – Mark Baker Apr 10 '17 at 08:24
-
Didn't know about w3schools reputation, thanks for the heads up. Luckily another user already provided the official docs. – Jdv Apr 10 '17 at 08:25
-
@Mark Baker I honestly don't know what people have against W3schools. They provide concise and useful - certainly if you're a beginner - intros to a lot of tech. Incidentally, using the + operator will ignore any value in the second array if it has a key present in the first array, even if the key is numeric. – Geoff Kendall May 23 '18 at 09:37
-
@GeoffKendall - In the past (I don't know if it's still true), w3schools was full of inaccuracies and recommendations that went against good practise.... that's what a lot of people have against w3schools – Mark Baker May 23 '18 at 09:47
-
2@GeoffKendall - But the actual PHP Docs will always be a better recommendation than a 3rd party site for information on a basic php function – Mark Baker May 23 '18 at 09:48
-
@Mark Baker Well, not always - the PHP docs sometimes miss showing a straightforward example. Have to admit I've just suggested a clarification over at W3schools on this particular operator's write up 'though. All the same, it's a free service and was useful to me when I was getting started - obviously, SO is the place once you're up and running. – Geoff Kendall May 23 '18 at 10:19
-
I can't think of many php functions that are missing examples; certainly array_merge has a number, including showing the index changes.... it is some of the best documentation you can find for almost any programming language – Mark Baker May 23 '18 at 10:37