All ways to combine arrays in php

There are several ways to combine arrays in php. The main functions of array_merge () and array_merge_recursive () work with complex arrays. The implode () method handles multidimensional arrays. There is a way to concatenate the elements of arrays and combine the elements of one array into a string.

Array_merge () function

Using this function in php combines arrays into a new separate array. Elements of the second array are at the end of the first. If the processed arrays have the same keys, the union will occur as follows. If the keys are string elements, the elements are replaced, and each subsequent value replaces the previous one.

If the elements with one key are numeric, then each element will be added to the end of the array. As a result, the values โ€‹โ€‹will be sorted by keys in ascending order.

First join example

Elements in the new array are arranged in such a way that the zero element will be the value with the color key, then the numbers 2 and 4, followed by a, b. Following them are shape => trapezoid and 4. Pay attention to elements with a common key. In this case, remains green.

Performance:

Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 ) 

Second example how to combine arrays in php.

The second example of unification

All elements are saved here, except for those that have a common key 3. In this case, the value of the first array remains. Result:

 array(5) { [0]=> string(6) "zero_a" [2]=> string(5) "two_a" [3]=> string(7) "three_a" [1]=> string(5) "one_b" [4]=> string(6) "four_b" } array_merge_recursive() 

Array_merge_recursive () function

With this function, you can recursively concatenate arrays in php. The values โ€‹โ€‹of one array go to the end of the other. Joining is the same as during the operation of the array_merge () function. The main difference is that absolutely all values โ€‹โ€‹are taken into account, including nested multidimensional arrays.

The third example of unification

In this case, the elements of arrays with string keys are combined. In this case, the following elements that have a common string key favorite are also combined. The numbers 5, 10 are added sequentially to the new array. The element color => array (...) becomes at the beginning of the resulting array, then goes 5 and 10. In this case, the blue value is removed, and red and green remain. Result:

 Array ( [color] => Array ( [favorite] => Array ( [0] => red [1] => green ) [0] => blue ) [0] => 5 [1] => 10 ) 

Implode () function

To combine an array in php into a string, the implode () method is used. Full syntax:

 string implode ( string $glue , array $pieces ) 

The $ glue string is empty by default; it is optional. The $ pieces array represents the elements that will eventually be merged. As a result, a string is returned with array elements between which there is a $ glue delimiter. Thus:

 $pieces[0] . $glue . $pieces[1] . $glue . $pieces[2] 

Conditionally, we can call the implode () function opposite to explode (), which splits the string into elements and moves them to an array.

The fourth example of unification

Here the elements of the array 'name', 'mail' and 'phone' will be combined into the string $ comma_separated. As a separator between elements, "," is used. Thus, the result is the string โ€œname, mail, phoneโ€.

Total

Three ways to combine two arrays in php are:

  • array_merge ();
  • array_merge_recursive ();
  • implode ().

The difference between the second method and the first is that the union happens recursively. That is, when combining multidimensional arrays, the elements of each nested array will be attached to each other. As a result, in both cases a new array is formed, consisting of two processed elements. In the process of execution, such features are manifested in which the values โ€‹โ€‹of one array are replaced by elements of another array. In other cases, it is possible that the elements are added simply to the end of the array.

The implode () function combines the elements of two arrays into a string. In the arguments, you can specify a character that will separate the words.

Source: https://habr.com/ru/post/K20692/


All Articles