Removing duplicates out of multiple arrays, php - php

I've got multiple arrays, and want to remove the duplicates. So I only got the unique items.
array(4) {
[0]=>
string(14) "Bergen op Zoom"
[1]=>
string(9) "jan steen"
[2]=>
string(7) "culture"
[3]=>
string(11) "Netherlands"
}
array(8) {
[0]=>
string(14) "fasion"
[1]=>
string(9) "conceptial"
[2]=>
string(7) "industrial"
[3]=>
string(11) "Netherlands"
}
I want to print all the strings out of the array except for the last Netherlands because it's already printed.
I've tried it with array_unique() but it only does that if there are duplicates in the array itself.
no clue how to get this thing working..

Use array_diff. It will stay in arr1 only items that not in arr2
array_diff($arr1, $arr2);

Try this
array_unique( array_merge($array1, $array2) );

Related

Php multidimensional array [matrix] sorting columns

If I have a matrix
[3,1,2,4]
[a,b,c,d]
And I need sort first row with usort key. But when I want reorder first array how do column moves at well
So output will be like this in this case described top
[1,2,3,4]
[b,c,a,d]
You can use array_multisort:
$x = [[3,1,2,4],['a','b','c','d']];
array_multisort($x[0], $x[1]);
var_dump($x);
Output:
array(2) {
[0]=>
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}
[1]=>
array(4) {
[0]=>
string(1) "b"
[1]=>
string(1) "c"
[2]=>
string(1) "a"
[3]=>
string(1) "d"
}
}
I think what you're looking for is ksort.

How can I detect the differences between two arrays?

I have two arrays, I would like to compare.
array1:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "dog"
["name"]=>
string(4) "alan"
}
["order"]=>
string(2) "12"
}
array2:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "fox"
["name"]=>
string(4) "tom"
}
["order"]=>
string(2) "12"
}
I compare them with array_diff:
$result = array_diff($array1, $array2);
But if I var_dump $result, I get the following output:
array(0) {
}
Does anyone have an idea why?
For associative arrays you should use array_diff_assoc. Also see the user contributed notes for how to do this recursively, if you need to.
With the help of sinaza I found out that no difference was displayed, because array_diff works different with multidimensional arrays.
Here is the code, that worked for me:
foreach ($array1 as $k1 => $v1) {
if (array_diff($array2[$k1], $array1[$k1])){
$result[$k1] = array_diff($array2[$k1], $array1[$k1]);
}
}

PHP json_encode - Strange behaviour

Using json_encode to encode an array of dates, it sometimes does one thing, sometimes does another.
For example, if I'm trying to encode something like:
array(6) {
[0]=>
string(6) "Jun-24"
[1]=>
string(6) "Jun-25"
[2]=>
string(6) "Jun-28"
[3]=>
string(11) "Training-24"
[4]=>
string(6) "Jun-29"
[5]=>
string(6) "Jun-30"
}
It will output
["Jun-24","Jun-25","Jun-28","Training-24","Jun-29","Jun-30"]
However, when I try to encode something like:
array(17) {
[0]=>
string(6) "Jun-23"
[1]=>
string(6) "Jun-24"
[2]=>
string(6) "Jun-28"
[3]=>
string(11) "Training-24"
[4]=>
string(6) "Jun-29"
[5]=>
string(6) "Jun-30"
[6]=>
string(6) "Jul-06"
[7]=>
string(6) "Jul-07"
[9]=>
string(6) "Jul-09"
[10]=>
string(6) "Jul-16"
[11]=>
string(6) "Jul-17"
[12]=>
string(6) "Jul-20"
[13]=>
string(6) "Jul-23"
[14]=>
string(6) "Jul-24"
[15]=>
string(6) "Jul-30"
[16]=>
string(6) "Aug-01"
[17]=>
string(6) "Aug-05"
}
It will output
{"0":"Jun-23","1":"Jun-24","2":"Jun-28","3":"Training-24","4":"Jun-29","5":"Jun-30","6":"Jul-06","7":"Jul-07","9":"Jul-09","10":"Jul-16","11":"Jul-17","12":"Jul-20","13":"Jul-23","14":"Jul-24","15":"Jul-30","16":"Aug-01","17":"Aug-05"}
(Sorry, couldn't find a smaller example where it fails)
Point being, why does it do this? The options are the same, the array is structured the same, what's the issue?
Your PHP array is missing entry 8, so is a mapping (object) and not a list (array).
You don't have key [8] set in your second example. According to the documentation a sequential array with an unset key will be encoded as a JSON object and not a JSON array.
In your first example the array is numbered sequentially from zero. PHP treats this as a conventional array and encodes it accordingly.
In your second example element 8 is missing. PHP treats this as an associative array and encodes the keys accordingly.
It is because of the indexing issue,
when your index is not proper it will behave like this.
the best way to resolve is reindexing.
$array = array_values($array);
Do like this just before converting to JSON.
Just when you encode the array, do this which will reindex array values:
$encoded = json_encode(array_values($myArray));

php merge, delete and keep order of two arrays

I have two arrays something like this
1) array(3) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" }
2) array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
now i want to merge and delete the double entries of the two arrays, important thing is here to keep the order of the first array in the final array (max,min,med -> from first array, and then all others from second array)
the two arrays have different lengths array(3) and array(4)
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
the problem is the order is lost
result:
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
what i need is this
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" [3]=> string(4) "other" }
You should erase the double entries from 2nd array first:
$array_last = array_diff($arr_last, $arr_first);
and then
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
Order is not lost, but if you want to sort array in alphabetical order use sort function
$myfinalarray=sort($myfinalarray);
Check the below code,
<?php
$arr1=array(0 => "max",1 => "min",2 => "med");
$arr3=array(0 => "max",1 => "min",2 => "other",3 =>"med");
$myfinalarray = (array_unique(array_merge($arr1, $arr3)));
print_r($myfinalarray);
?>
the output willbe like,
Array
(
[0] => max
[1] => min
[2] => med
[5] => other
)
check here

Sort times array and keep unique values

I have a $times array, which contains:
array(8) {
[0]=>
string(5) "10:00"
[1]=>
string(5) "13:00"
[2]=>
string(5) "10:00"
[3]=>
string(5) "11:00"
[4]=>
string(5) "12:00"
[5]=>
string(5) "13:00"
[6]=>
string(5) "14:00"
[7]=>
string(5) "15:00"
}
How can I a) sort it so it starts with lowest value b) only have one entry of each time? (no duplicates, currently theres two 10:00s and 13:00s etc)
Why not just use PHP's inbuilt functions:
$input = array_unique($input);
sort($input);
print_r($input);
Well, you could always use the php-shipped standard functions for arrays:
Array Unique
and sort()
$array = array_unique(sort($a));

Categories