Checking Duplicate Value in each array in PHP - php

I have a question. Let say I have three or more arrays:
[weekly] => Array (
[1] => Array
(
[0] => 11:00
[1] => 11:00
)
[2] => Array
(
[0] => 13:00
[1] => 16:00
)
[5] => Array
(
[0] => 08:00
[1] => 12:00
[2] => 15:00
)
...
)
I tried to use array_unique() in foreach() loop but I can't catch that the first array has duplicate value. Actually, I would like to check if one of the array in the weekly array has a duplicate value and will return an error message that one of the arrays inside the weekly array has a duplicate value.
What is the proper checking of each sub-arrays of [weekly] that the first array has a duplicate value? I'm doing this using PHP.
Your answers will be highly appreciated. Thanks

The correct way to use array_unique() is not in a foreach loop.
<?php
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);
?>
Code taken from php documentation http://php.net/manual/en/function.array-unique.php

To see if there is a difference between two arrays, you can use count() after applying array_unique().
So, make a loop over a duplicate object, unique it out. Then check if the count is the same as the original within the same loop.
This can be done in a more optimised way, but this is a start.

Related

array difference works not correct

I have 2 array which I compare to get the difference:
Array 1
(
[salutation] =>
[fname] => Max
[lname] => Mustermann
)
Array 2
(
[salutation] => Herr
[fname] => Max2
[lname] => Mustermann2
)
I compare them like this:
$keys = array_keys(array_diff($array1,$array2));
And the result:
Array
(
[0] => fname
[1] => lname
)
Which is correctly but I miss "salutation". It is different, too.
Where is my mistake?
The definition of array_diff is:
array_diff(array $array, array ...$arrays): array
Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.
Comparison is done by value. salutation doesn't actually have a value in the source array, so it is not included in the result.
Consider using array_diff_assoc.
If you watch on the PHP documentation here: https://www.php.net/manual/en/function.array-diff.php
You can see that it checks every item in the array (first argument in array_diff) that has a value. Since salutation in array1 is null it doesn't get compared. Hope this helps you out!
Greetings :)

PHP - Why doesn't array_multisort function sort correctly?

please view the following code with 2 arrays. i use multisort function with sort flags for ascending and numeric then display. as you can see in the output that array 2 starts with 100 when it should be last. please explain what is causing this and how to sort it correctly. thank you.
<?php
$array1 = array(1,7,10,6);
$array2 = array(100,20,25,10);
array_multisort($array1, SORT_ASC, SORT_NUMERIC, $array2);
print_r($array1);
echo "<br>";
print_r($array2);
?>
output:
Array ( [0] => 1 [1] => 6 [2] => 7 [3] => 10 )
Array ( [0] => 100 [1] => 10 [2] => 20 [3] => 25 )
Ah, yes, array_multisort is a bit tricky to understand the first time round.
Basically the sort is lexicographical, a fancy word meaning that the first array is sorted and the second arrays elements are ordered according to the first array.
Look at your first (output) array and see the order and map it to the initial second array and you'll see whats happening.
So the second array you take the 1st, 4th , 2nd and 3rd elements.
If you just want plain sorting for multiple arrays then just do them one by one or over a loop.

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

comparing 2 arrays 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

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