comparing 2 arrays php - php

I am trying to compare two different arrays and get the values that do not exist in 1 of the arrays. Here are my 2 arrays:
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52)
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52 [3] => 583cee91-1913-4e9d-b51d-e27083420001)
As you can see the second array has an additional value. I am trying to user array_diff like this:
$result = array_diff($array1,$array2);
print_r($result);
However the out of the array_diff is:
array()
Any ideas what is going on?
As people have suggested and i have already tested switching the arrays around, this is the output:
Array ( [0] => [1] => )

array_diff gives you the values from $array1 that are not in the other arrays. All the values of your first array are in the second. Sou change the order of your arrays and you should be fine.
See also here: http://php.net/manual/de/function.array-diff.php

The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays2
Read array_diff

$result = array_diff($array2,$array1);
Try like this

Related

get difference from three arrays by value

I am trying to get the difference between three arrays
array_diff() function doesn't help because it matches only the first array with others
For example I want to compare three arrays and each array has one of the element different from each other like this
$a1=array("a"=>"red","b"=>"green","c"=>"blue", 'v1' => 'sss');
$a2=array("e"=>"red","f"=>"green","g"=>"blue", 'v2' => 'ss');
$a3=array("e"=>"red","f"=>"green","g"=>"blue", 'v3' => 's');
when I use array_diff() on these arrays it would just show me the unique value out of other two arrays
$res = array_diff($a1, $a2, $a3);
print_r($res);
It's result would be Array ( [v1] => sss )
While I want it to tell me the unique values in all these arrays like this Array ( [v1] => sss [v2] => ss [v3] => s )
I tried other array comparison functions but couldn't find one to compare all given array to each other instead of comparing just one array to others
$var=(array_diff($a1,$a2,$a3));
$var1=(array_diff($a2,$a1,$a3));
$var2=(array_diff($a3,$a2,$a1));
$v=array_merge($var,$var1,$var2);
I guess this helps for you.check it out..

Displaying result of first array in array_diff

I am using codeigniter.
I want difference of two array as I am using array_diff function of php.
Due to associative array, I have used call_user_func_array and I got record.
$result_sun = call_user_func_array('array_merge', $data['sun_holiday']);
$result_sat = call_user_func_array('array_merge', $data['third_sat']);
But when I am going to make difference of these two array like,
$result = array_diff($result_sun,$result_sat);
It only shows the record of first array $result_sun.
$result_sun = Array
(
[0] => 2015-09-06
[1] => 2015-09-13
[2] => 2015-09-20
[3] => 2015-09-27
)
$result_sat = Array
(
[0] => 2015-09-19
)
So, why the difference is not occurring??
$result1 = array_diff($result_sun,$result_sat);
$result2 = array_diff($result_sat,$result_sun);
$result=array_merge($result1,$result2);
Compares $result_sun against one or more other arrays and returns the values in $result_sun that are not present in any of the other arrays.
so take difference of both and then merge it will be good if you put your code then we can give more accurate answer

Array_diff, how do i compare string and values of two different array

I want to get the array_diff of these two array and echo by key
$s1=1;
$s2=7;
$s3=8;
$r=array("$s1","$s2","$s3");
$rr=array("1","2","3","4","5","6","7","8","9","10");
$rrr=array_diff($r,$rr);
echo $rrr[0];
I was hoping to get a result like 2 but i got an error. Someone help out here.
The documentation for array_diff says that it
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
Your array1 is $r, and your other array is $rr.
$r is essentially equivalent to
$r = array("1","7","8");
We can clearly see that there are no values in $r that are not present in $rr. ("1", "7", "8" are all in $rr.)
Thus, you will receive a E_NOTICE when you try to access $rrr[0], because $rrr is empty.
Perhaps you meant to reverse the order of the two arguments.
$rrr = array_diff($rr, $r);
As Loop Me pointed out, array_diff does not reorder your indices.
What this means is that your array is now like this.
Array
(
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[8] => 9
[9] => 10
)
You can reorder them with array_values.
$rrr = array_values($rrr);
Demo

PHP Aligning Array Key Values

I've Googled it for two days, and tried looking at the PHP manual, and I still can't remember that function that aligns the key values for PHP arrays.
All I'm looking for is the function that takes this:
Array
(
[0] => 1
[3] => 2
[4] => 3
[7] => 4
[9] => 5
)
And converts it into this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Basically, the array is first sorted by key (their values attached to them stay with them), then all the keys are set to all the counting numbers, consecutively, without skipping any number (0,1,2,3,4,5,6,7,8,9...). I saw it being used with ksort() a few months ago, and can't see to remember or find this elusive function.
Well, you see, this one is hard, because the general description on the PHP array functions page does not say that this function does what you're looking for.
But you can sort the array using ksort(), and then use this: array_values() . From the page from the PHP manual:
array_values() returns all the values from the input array and indexes numerically the array.
You can use array_merge:
$array = array_merge($array);
It will reindex values with numeric keys.
Update: Using array_values as proposed in #LostInTheCode's answer is probably more descriptive.
function array_reset_index_keys($array)
{
$return = array();foreach($array as $k => $v){$return[] = $v;}return $return;
}
And then use like a regular function, should re index the array
you can also use native functions such as array_values which returns the values of an array into a single dimension array, causing it to be re indexed .

How to reset indexes in array_diff result?

I have two arrays: Array ( [0] => 2 [1] => 3 ) and Array ( [0] => 2 ).
I want to get the value, which is not in second array. So I have used the array_diff function but my result will get Array ( [1] => 3 )
Actually this is the result. But a small problem here, its position is (key) 1. I want the result in to a new array starts from 0th position, i.e., Array ( [0] => 3 ).
How can I achieve this?
you can use array_values(array_diff($arr1, $arr2)); if order doesn't matter
You should run array_values() on the result and this would give you a new array with indexes starting at 0.
This is a known shortcoming of array_diff(), check the php docs.

Categories