Reputation: 6389
I have an array of arrays like this:
Array
(
[userId] => 35
[fieldId] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 1
[4] => 2
[5] => 2
[6] => 1
[7] => 4
)
[educationTitle] => Array
(
[0] => School1
[1] => School2
[2] => 3
[3] => School1
[4] => School2
[5] => School2
[6] => School1
[7] =>
)
)
I want to remove all duplicates of each array. So, I want the final array to look like this:
Array
(
[userId] => 35
[fieldId] => Array
(
[0] => 1
[1] => 2
[2] => 3
[7] => 4
)
[educationTitle] => Array
(
[0] => School1
[1] => School2
[2] => 3
[7] =>
)
)
I've tried this (as recommended in this answer https://stackoverflow.com/a/307701/1009116):
function multi_unique($updates) {
foreach ($updates as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
And it has no effect
I also tried this (as recommended here - https://stackoverflow.com/a/946300/1009116)
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
And this just returns the last array (however, it is filtered as it should be)
What am I doing wrong???
Upvotes: 2
Views: 187
Reputation: 7870
Another way using array_flip();
<?php
$myArray = array( "userID"=>'35',
"fieldID" => array(
"0" => '1',"1" => '2',
"2" => '3',"3" => '1',
"4" => '2',"5" => '2',
"6" => '1',"7" => '4'),
"educationTitle"=>array(
"0"=>'School1',"1"=>'School2',
"2"=>'3',"3"=>'School1',
"4"=>'School2',"5"=>'School2',
"6"=>'School1',"7"=>'',),);
print_r($myArray);
$myArray['fieldID'] = array_flip($myArray['fieldID']);
$myArray['fieldID'] = array_flip($myArray['fieldID']);
$myArray['educationTitle'] = array_flip($myArray['educationTitle']);
$myArray['educationTitle'] = array_flip($myArray['educationTitle']);
print_r($myArray);
?>
Final Output
Array
(
[userID] => 35
[fieldID] => Array
(
[6] => 1
[5] => 2
[2] => 3
[7] => 4
)
[educationTitle] => Array
(
[6] => School1
[5] => School2
[2] => 3
[7] =>
)
)
Upvotes: 0
Reputation: 95101
You can use :
$data = array(
'userId' => 35,
'fieldId' => array(
0 => 1,
1 => 2,
2 => 3,
3 => 1,
4 => 2,
5 => 2,
6 => 1,
7 => 4
),
'educationTitle' => array(
0 => 'School1',
1 => 'School2',
2 => 3,
3 => 'School1',
4 => 'School2',
5 => 'School2',
6 => 'School1',
7 => NULL
)
);
print_r(arrayUnique($data));
Output
Array
(
[userId] => 35
[fieldId] => Array
(
[0] => 1
[1] => 2
[2] => 3
[7] => 4
)
[educationTitle] => Array
(
[0] => School1
[1] => School2
[2] => 3
[7] =>
)
)
Function Used
function arrayUnique($array) {
$input = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ( $input as $key => $value ) {
is_array($value) and $input[$key] = arrayUnique($value);
}
return $input;
}
Upvotes: 3
Reputation: 173552
You could use array_combine()
in this case; later you pull apart the data into their respective containers.
$combined = array_combine($arr['fieldId'], $arr['educationTitle']);
$arr['fieldId'] = array_keys($combined);
$arr['educationTitle'] = array_values($combined);
Do note that the original indices are renumbered after this operation.
Upvotes: 3