This question already has answers here:
php array difference
(7 answers)
Closed 7 years ago.
I dispose of two arrays with the following contents:
$arr1 = "one", "two", "three";
$arr2 = "one", "two";
I want to make sure to return an array containing only the value "three";
In my applications in fact the values of $arr1 represent the link to be included in a table, those of $arr2 links already entered.
I tried using the function intersect but this returns the values that are duplicates, but I want to discard the duplicates and take only the values that are not present in $arr2;
$c = array_intersect($arr1, $arr2);
$filter= array_diff($arr1 , $arr2 );
Related
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
This question already has answers here:
PHP - Check if two arrays are equal
(19 answers)
Closed 5 years ago.
I have two arrays i want to match exact both keys and values of arrays one of array be three dimensional or more
Example
$arr1 = ['status'=>true,'message'=>'data saved'];
$arr2 = ['status'=>true,'message'=>'data saved'];
in this scenario array return 1 but they are not equal
$arr1 = array("messagess"=>"data added","status" => true);
$arr2 = array("status" => true,'message'=>'data has been added');
echo count(array_intersect_assoc($arr1,$arr2));
Expected should be true if both exact match otherwise false. I have tried array_intersect() and other methods but failed.
Please Guide!
Thank in Advance
You could use array_intersect_assoc() and count the resulting number
echo count(array_intersect_assoc($arr1,$arr2));
http://php.net/manual/en/function.array-intersect-assoc.php
If the number in count is the same of the number of index keys you want check the the two array are the same otherwise you get the number of key values that match
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove values from an array in PHP?
I have this
$arr1 = array('orange','banana');
$arr2 = array('broccli','tomato','mixedvegies','orange','veg2');
how would I take complete $arr1 from $arr2 so that arr2 has
$arr2 = array('broccli','tomato','mixedvegies','veg2');
array_diff($arr2, $arr1);
array_diff returns an array with all the elements not in the second (or third, fourth... optional args).
You can use array_diff for this:
$uniqueItems = array_diff($arr2, $arr1);
This question already has answers here:
Create an assoc array with equal keys and values from a regular array
(3 answers)
Closed 6 years ago.
I have this array:
$a = array('b', 'c', 'd');
Is there a simple method to convert the array to the following?
$a = array('b' => 'b', 'c' => 'c', 'd' => 'd');
$final_array = array_combine($a, $a);
Reference: http://php.net/array-combine
P.S. Be careful with source array containing duplicated keys like the following:
$a = ['one','two','one'];
Note the duplicated one element.
Be careful, the solution proposed with $a = array_combine($a, $a); will not work for numeric values.
I for example wanted to have a memory array(128,256,512,1024,2048,4096,8192,16384) to be the keys as well as the values however PHP manual states:
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.
So I solved it like this:
foreach($array as $key => $val) {
$new_array[$val]=$val;
}