array_diff not returning. why? - php

I've read some tutorials on here but none of them return what I need. I have two arrays.
$a = '5,6,';
$b = '6,6,';
$a_array = explode(',',$a);
$b_array = explode(',',$b);
$result = array_diff($b_array,$a_array);
var_dump($result);
However, when I run array_diff, it returns an empty array.
$result = array_diff($b_array,$a_array);
But I'd like it to return 6. What's the error in my code?

It is because you are comparing the b to the a. The b array contains no elements that are different from elements in a. All the similar elements are not compared to elements in similar positions. They are compared to all elements from the opposite array. If you compared a to b, you would return the 5 value in the result array, because no elements in b have 5. But when you compare b to a, each 6 finds a pair in a, so no differences are found.
$a = '5,6,';
$b = '6,6,';
$a_array = explode(',',$a);
$b_array = explode(',',$b);
$result = array_diff($a_array,$b_array);
var_dump($result);

array_diff() doesn't work like this.
array_diff($b_array,$a_array) would return an array with entries that are present in $b_array but not present in $a_array.
So, in your case, it would check if $a_array contains '6' or not two times( as $b_array would be having two '6') and will found out each time that yes, $a_array contains'6' and hence returns empty array.
For more insights: https://www.w3schools.com/php/func_array_diff.asp

Related

return an array of numbers that show up in all three sub arrays

I want to compare all the sub arrays in the $ids and I want to return only the numbers that show up in all three arrays, so it would return 3 and 4 as an array.
$ids = [
[1,2,3,4],
[2,3,4],
[3,4,5],
];
expecting it to return
array(3,4)
UPDATE: My apologies, I left out the detail that I would not know how many arrays would be in the associative array, it could be 2 sub arrays, other times it could be 5. I appreciate all the answers I have received.
Obviously array_intersect is the solution How to get common values from two different arrays in PHP. If you know the number of sub-arrays and their indexes then it's as simple as:
$result = array_intersect($ids[0], $ids[1], $ids[2]);
However, if you need to do it for an unknown/variable number of sub-arrays and/or unknown indexes, use $ids as an array of arguments with call_user_func_array:
$result = call_user_func_array('array_intersect', $ids);
Or better use Argument unpacking via ... (PHP >= 5.6.0):
$result = array_intersect(...$ids);
Splice out the first subarray and then loop the rest and use array_intersect to filter the result to 3,4.
$res = array_splice($ids,0,1)[0];
foreach($ids as $id){
$res = array_intersect($res, $id);
}
var_dump($res); // 3,4
https://3v4l.org/Z7uZK

Sorting a non-associative, multidimensional array

I found lots of info about sorting associative arrays but little about sorting non-associative ones. My array is structured/populated this way:
$my_array = array();
$my_array[0][0] = 'whatever3';
$my_array[0][1] = 3
$my_array[1][0] = 'whatever2';
$my_array[1][1] = 2
$my_array[2][0] = 'whatever1';
$my_array[2][1] = 1
I want to sort it by the second value to get:
$my_array[0][0] = 'whatever1';
$my_array[0][1] = 1;
$my_array[1][0] = 'whatever2';
$my_array[1][1] = 2;
$my_array[2][0] = 'whatever3';
$my_array[2][1] = 3;
How can this be achieved considering my array isn't associative?
What about:
usort($combined, function ($a, $b) { return $a[1] - $b[1]; });
With usort you provide a custom comparison function that must return:
0, if the elements must be considered equal.
a negative number, if the first element must be considered smaller than the second one.
a positive number, if the first element must be considered greater than the second one.
In this case, we choose to compare the second element of each item of $combined array.

PHP : Getting not matched records from 2 arrays

Below are the 2 arrays
$full = array('ABCD19ed81424931667', 'ABCD0c08b1424947569');
$filtered = array('ABCD19ed81424931667');
I want the records which are not matching in 2 arrays. I have tried
array_diff, array_diff_assoc functions it did not work for me.
array_diff returns the difference in 1 direction. To get the difference in both directions you can do 2 array_diff followed by a array_merge:
$output = array_merge(array_diff($full, $filtered), array_diff($filtered, $full));
array_diff — Computes the difference of arrays.
Here array_diff(A,B) and array_diff(B,A) is different.
$full = array('ABCD19ed81424931667', 'ABCD0c08b1424947569');
$filtered = array('ABCD19ed81424931667');
$result = array_merge(array_diff($full, $filtered), array_diff($filtered, $full));
array_diff(A,B) returns all elements from A, which are not elements of B (= A without B). so you need to merge output

How do I make a new array containing just one instance of duplicates from 2 separate arrays while combining them in php?

So far I have done the following:
$row = mysql_fetch_assoc($result);
$row2 = mysql_fetch_assoc($result);
$duplicates = array_intersect($row, $row2);
How do I combine the 2 arrays and make a new one that just contains one instance of the previously repeated variables? (so if array $row contained the variable 'apple' 2 times and the array $row2 contained the variable 'apple' 3 times, in the new, merged array, 'apple' would only appear once.
edit: I didn't realize that the array_merge() function works differently for numbers than compared to strings. I gave the 'apple' example above but my arrays are dealing with product IDs which are numbers. PHP manual says
If, however, the arrays contain numeric keys, the later value will not
overwrite the original value, but will be appended.
and I need help in merging arrays with numbers, what should I do?
You can use
$c = array_merge($a, $b);
http://php.net/manual/en/function.array-merge.php
if you want to remove duplicate values after the merge then use array_unique();
so...
$c = array_unique(array_merge($a, $b));

How to compare two arrays and remove matching elements from one for the next loop?

How else might you compare two arrays ($A and $B )and reduce matching elements out of the first to prep for the next loop over the array $A?
$A = array(1,2,3,4,5,6,7,8);
$B = array(1,2,3,4);
$C = array_intersect($A,$B); //equals (1,2,3,4)
$A = array_diff($A,$B); //equals (5,6,7,8)
Is this the simplest way or is there a way to use another function that I haven't thought of? My goal is to have an array that I can loop over, pulling out groups of related content (I have defined those relationships elsewhere) until the array returns false.
You've got it. Just use array_diff or array_intersect. Doesn't get much easier than that.
Edit:
For example:
$arr_1 = array_diff($arr_1, $arr_2);
$arr_2 = array_diff($arr_2, $arr_1);
Source
Dear easy and clean way
$clean1 = array_diff($array1, $array2);
$clean2 = array_diff($array2, $array1);
$final_output = array_merge($clean1, $clean2);
See also array_unique. If you concatenate the two arrays, it will then yank all duplicates.
Hey, even better solution: array _ uintersect.
This let's you compare the arrays as per array_intersect but then it lets you compare the data with a callback function.
Try to this
$a = array(0=>'a',1=>'x',2=>'c',3=>'y',4=>'w');
$b = array(1=>'a',6=>'b',2=>'y',3=>'z');
$c = array_intersect($a, $b);
$result = array_diff($a, $c);
print_r($result);

Categories