All ways how to merge arrays in php

Table of contents:

All ways how to merge arrays in php
All ways how to merge arrays in php
Anonim

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

Array_merge() function

This function in php combines arrays into a new separate array. The 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 the same 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
First join example

The elements in the new array are lined up in such a way that the zero element will be the value with the color key, then the numbers 2 and 4, after them a, b. They are followed by shape=> trapezoid and 4. You should pay attention to the elements withcommon key. In this case, it remains green.

Execution:


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

Second example how to merge arrays in php.

Second join example
Second join example

All elements are stored here, except for those that have a common key of 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

This function allows you to recursively concatenate arrays in php. The values of one array go to the end of another. Joining occurs in the same way as in the course of the array_merge() function. The main difference is that absolutely all values are taken into account, including nested multidimensional arrays.

Third join example
Third join example

In this case, array elements 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 sequentially added to the new array. The element color=> array(…) becomes the beginning of the resulting array, then comes 5 and 10. In this case, the blue value is removed, leaving red and green. Result:


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

Implode() function

To concatenate an array in php into a string, use the implode() method. Full syntax:


string implode (string $glue, array $pieces)

The $glue string is empty by default and is optional. The $pieces array represents the elements that will eventually be combined. As a result, a string with array elements is returned, between which there is a separator $glue. This way:


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

Conventionally, the implode() function can be called the opposite of explode(), which splits the string into elements and moves them into an array.

Fourth join example
Fourth join example

Here the array elements 'name', 'mail' and 'phone' will be combined into the string $comma_separated. ", " is used as a separator between elements. Thus, the result is the string “name, mail, phone number”.

Result

Three ways to merge two arrays in php are:

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

The second method differs from the first one in that the union is recursive. That is, when merging 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 the elements of the two processed. During execution, such features appear in which the values of one array are replaced by elements of another array. In otherscases, it is possible that elements are simply added to the end of the array.

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

Popular topic

Editor's choice

  • IPv6 protocol: setup on Windows systems
    IPv6 protocol: setup on Windows systems

    Probably, many users of computer systems, delving into the network settings, noticed that in the list of protocols, in addition to the well-known IPv4, there is also the sixth version (IPv6)

  • View history on computer
    View history on computer

    Today you can learn about almost all the actions taken on the computer. It's about browsing history

  • Computer technology hardware: definition, description and types
    Computer technology hardware: definition, description and types

    Modern computers to ensure maximum performance and correct operation use hardware and software that are very interconnected and clearly interact in different directions. Now let's touch on the consideration of hardware, since initially it is they who occupy a dominant position in ensuring the operability of any computer or even mobile system

  • Keyboard shortcuts and the evolution of operating systems
    Keyboard shortcuts and the evolution of operating systems

    It takes about a minute to copy or move a file from one location to another using the menu system. The developers introduced a keyboard shortcut that replaced all these actions and made it possible to reduce the time of this common operation by 3 or more times. True, for this it was necessary to slightly modernize the keyboard: new keys were added - "Ctrl", and later - "Windows"

  • If the torrent does not download
    If the torrent does not download

    An article for those who consider the ability to download files from torrent trackers a convenience, not an echo of the past. Not only the unavailability of "high-speed" tariff plans is the reason for the choice, but also personal preferences