i know that this question is answered more times here. but i can't figure out how to do this
i have an array with arrays in IT
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
[1] => 100
[2] => 94
)
)
as you see in 4 and 15 keys we have duplicates (94 and 100)
i want to remove all this duplicates except one
so in final to have
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
)
)
Big thanks
CodePudding user response:
Loop over your outer array, inside loop over your inner arrays.
Check if the current value is already contained in a helper array, if so unset the array element (using the outer and inner level key.)
Then add the current value to the helper array, so that it can be recognized as a duplicate value when it occurs the next time.
$data = [
11 => [3, 4, 5, 37, 41],
4 => [67, 74, 80, 82, 94, 95, 100],
15 => [86, 100, 94]
];
$helperArray = [];
foreach($data as $dkey => $item) {
foreach($item as $ikey => $value) {
if(in_array($value, $helperArray)) {
unset($data[$dkey][$ikey]);
}
$helperArray[] = $value;
}
}
print_r($data);
CodePudding user response:
Here is the most un-elegant solution but it will always do the trick...
<?php
/* Create the array */
$array[11][] = 3;
$array[11][] = 4;
$array[11][] = 5;
$array[11][] = 37;
$array[11][] = 41;
$array[4][] = 67;
$array[4][] = 74;
$array[4][] = 80;
$array[4][] = 82;
$array[4][] = 94;
$array[4][] = 95;
$array[4][] = 100;
$array[15][] = 86;
$array[15][] = 100;
$array[15][] = 94;
/* print the complete array : */
echo '<pre>';
print_r($array);
echo '<pre>';
/* use a simple array to store values as you loop */
$A = [];
foreach($array as $key => $ar){
foreach($ar as $k => $a){
/* if the value already appeared somewhere... remove it from this and next locations...*/
if(in_array($a,$A)){
unset($array[$key][$k]);
}
$A[] = $a;
}
}
/* Print our cleaned array. */
print_r($array);
will return:
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
[1] => 100
[2] => 94
)
)
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
)
)
Next time , please share your code, as bad as it can be or whatever it shows your way and that you have tried solving this for real...
CodePudding user response:
I made the data a bit more complex, so that the duplicates are not only in the array elements with the keys 4 and 15.
$data = [
11 => [ 3, 4, 94, 5, 80, 37, 41 ], // will be: 3, 4, 94, 5, 80, 37, 41
4 => [ 67, 74, 80, 82, 95, 100 ], // will be: 67, 74, 82, 95, 100 (80 > key 11)
7 => [ 2, 4, 77, 81, 13, 100 ], // will be: 2, 77, 81, 13 (4 > key 11, 100 > key 4)
15 => [ 86, 100, 94, 13 ] // will be: 86 (100 > key 7, 94 > key 11, 13 > key 7)
];
$keys = array_reverse(array_keys($data));
for ($i = 0; $i < count($keys); $i ) {
$keyI = $keys[$i];
for ($j = $i 1; $j < count($keys); $j ) {
$keyJ = $keys[$j];
$diff = array_diff($data[$keyI], $data[$keyJ]);
$data[$keyI] = $diff;
}
}
print_r($data);
