In above example we have covered about merging two array with unique values and same applied to multidimensional array. Print_r(array_unique(array_reduce($data, 'array_merge', ))) We know that the array_reduce iteratively reduce the array to a single value using a callback function and then values are merged to empty array and finally in return result we get the unique values using array_unique If you assign only one array to the arraymerge recursive () function, it will behave exactly the same as the array. 'green' array2 'color' > 'red', 'color' > 'blue' result arraymerge(array1, array2) > // result 'color' > 'green.When two or more array elements have the same key, instead of override the keys, the arraymerge recursive () function makes the value as an array. The array merge multidimensional array with unique values. The arraymerge recursive () function merges one or more arrays into one array. $array = array_unique (array_merge ($a, $b)) are the arrays you wish to merge One or more arrays should be supplied. The array_merge will be used to merge two arrays and return final result in array.Īrray unique returns array of distinct values $a = array('a' => 1, 'b' => 2, 'c' => 3) A couple of weeks ago I came across a way to merge arrays in PHP with the + operator, instead of using the array functions (e.g. arraymerge will return an array containing the contents of the arrays passed to it merged into a single array array1, array2, etc. You can rate examples to help us improve the quality of examples. Example #1īelow example we are using combination of two native functions array_merge and array_unique. These are the top rated real world PHP examples of arraymerge extracted from open source projects. One common operation in any programming language includes merging and we are going to implement here merge two PHP array and have only unique values from both.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |