Merge Two Aarrys [duplicate] - php

This question already has answers here:
PHP - Merging two arrays into one array (also Remove Duplicates)
(9 answers)
Closed 3 years ago.
I have two arrays one is the superset of other,how to create a third array which has all the values of both arryas but not repeating
me please
$a = array(1,2,3);
$b = array(1,2,3,4);
output must be like this
$c = array(1,2,3,4);

use array_merge() along with array_unique()
array_unique(array_merge($a,$b));
Output:-https://3v4l.org/lfm1b
Note:- if you want to re-index array use array_values() [added in my working example link]
Reference:
array_values()

$c = array_unique(array_merge($a, $b));
All of this can be seen in: http://php.net/manual/en/function.array-merge.php

Use array_merge with array_unique
print_r(array_unique(array_merge($a,$b)))

Use + sign to merge them
$c = $a + $b;
Working example :- https://3v4l.org/5P2LP

Related

how do I merge arrays in php even if one of those arrays is null? [duplicate]

This question already has answers here:
If null use other variable in one line in PHP
(13 answers)
Best way to give a variable a default value (simulate Perl ||, ||= )
(8 answers)
Closed 3 years ago.
Riht now I use array_merge_recursive but if one of my 3 arrays is null I get for echo json_encode($array4);
null
I have 3 arrays in my php file:
$array1 = json_decode($array1, TRUE);
$array2 = json_decode($array2, TRUE);
$array3 = json_decode($array3, TRUE);
If I echo each of the arrays:
echo json_encode($array1); = {"results":[{"cat_id":2,"cat_name":"bicycle repairs"}]}
echo json_encode($array2); = {"results":[{"cat_id":"4","cat_name":"plumber"},{"cat_id":"5","cat_name":"Electrician"},{"cat_id":"6","cat_name":"vet"}]}
echo json_encode($array3);= {"results":[{"cat_id":3,"cat_name":"Doctor"}]}
And then I merge these arrays together like this:
$array4 = array_merge_recursive($array1['results'], $array2['results'], $array3['results']);
Which would give me:
[{"cat_id":2,"cat_name":"bicycle repairs"},{"cat_id":"4","cat_name":"plumber"},{"cat_id":"5","cat_name":"Electrician"},{"cat_id":"6","cat_name":"vet"},{"cat_id":3,"cat_name":"Doctor"}]
But if any of $array1, $array2 or $array3 is null then $array4 doesn't work. How can I overcome this?
I'm afraid that the library only admits arrays as arguments (https://www.php.net/manual/en/function.array-merge-recursive.php).
So if you want merge all elements I advise you that use ?? (Null Coalescing operator) that return the first params if isset and is not null, you can do something like this:
array_merge_recursive($array1['results']??[], $array2['results']??[], $array3['results']??[])
I would make a helper function to check if you variable is an Array. If not just make it an empty one
function MakeArray($arr){
if (!is_array($arr)) return [];
return $arr;
}
$arr1 = MakeArray([1,2,4]);
$arr2 = MakeArray([5,6]);
$arr3 = MakeArray(NULL);
This way you can guarantee that it will work even if you pass, for example, a string

how to remove duplicate elements from an array of arrays in PHP [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 5 years ago.
"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"]
"2017-08-22":["5948a0dd21146a43fdcfef5a"]
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
I have an array like this, key is a date and multiple value associated with that date, but I need unique value with that key.how to do that ?
I've tried with array_unique but no luck!
Just use array_map and array_unique together.
<?php
$a = json_decode('{"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"],
"2017-08-22":["5948a0dd21146a43fdcfef5a"],
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"],
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]}', true);
echo json_encode(array_map("array_unique", $a));
$array = your array
$array = array_map(function($val){return array_unique($val);}, $array);
You better do this using SQL if it's possible. Otherwise, you can try this:
$array = array_unique($array, SORT_REGULAR);

PHP array associative [duplicate]

This question already has answers here:
PHP combine two associative arrays into one array
(8 answers)
Closed 5 years ago.
Whats the easiest way to convert array a to b
a= [['x'=>'a'], ['y'=>'b']]
b= ['x'=>'a', 'y'=>'b']
a and b are just two examples.
Using array_walk_recursive for arbitrary depth:
$b = [];
array_walk_recursive($a, function ($v, $k) use (&$b) { $new[$k] = $v; });
Using #splash58 trick with the spread operator if you have only one level deep:
$b = array_merge(...$a);

PHP Merge two indexed arrays but NOT normally [duplicate]

This question already has answers here:
Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion
(2 answers)
Closed last month.
I have two arrays:
$array1 = array("A","One","C","Z");
$array2 = array("B","K","2","5");
Is there some built-in PHP way to get a final array with (I don't know how to say it, maybe 1-1 correspondence addition) alternate keys appended to each other like this
$final_array = array("A","B","One","K","C","2","Z","5");
If I use array_merge, I get:
$final_array = array("A","One","C","Z","B","K","2","5")
But that's exactly what an array_merge does. Is there any workaround other than looping?
Try this:
$array1 = array(1,3,5,7);
$array2 = array(2,4,6,8);
$final_array = array_merge($array1,$array2);
sort($final_array);
print_r($final_array);
Hope this helps. Sorted the array using the sort function.

How can i get the common value from same array [duplicate]

This question already has answers here:
Eliminating duplicate values using PHP
(3 answers)
Closed 9 years ago.
I want to get the common value as well as different value from same array
For example :
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
And I want the array as
$array1 = array(1,2,3,4,5,6,7,8,9,10);
Can anyone give me the idea to bring the array like this
Why don't you try
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$array1 = array_unique($array1);
http://php.net/manual/en/function.array-unique.php
Use array unique
array_unique($array1);
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$array = array_unique($array1);
print_r($array )
see array_unique . It will remove the Duplicate values from array
try this :
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$unique_array=array_unique($array1);
print_r($unique_array);
use array_unique(). It reduceds an array.

Categories