Reputation: 514
I've tried a lot of solutions, but none of them worked with my case. For example I have 3 arrays (the amount of them can vary from 1 to 4) and I want to delete all the duplicate values from multidimensional array
Example
Array
(
[33] => Array
(
[product_id] => 33
[name] => Aliquam dolor tellus Aliquam dolor tellus
[color] => Brown
[breat] => 3
[shorten] => Yes
[material] => Natural
[rigidity] => Low
)
[54] => Array
(
[product_id] => 54
[name] => New Style
[color] => Black
[breat] => 4
[shorten] => Yes
[material] => Natural
[rigidity] => Low
)
[23] => Array
(
[product_id] => 23
[name] => New Natural
[color] => Yellow
[breat] => 5
[shorten] => No
[material] => Natural
[rigidity] => Low
)
)
And here is the desired array.
Array
(
[33] => Array
(
[product_id] => 33
[name] => Aliquam dolor tellus Aliquam dolor tellus
[color] => Brown
[breat] => 3
[shorten] => Yes
[material] =>
[rigidity] =>
)
[54] => Array
(
[product_id] => 54
[name] => New Style
[color] => Black
[breat] => 4
[shorten] => Yes
[material] =>
[rigidity] =>
)
[23] => Array
(
[product_id] => 23
[name] => New Natural
[color] => Yellow
[breat] => 5
[shorten] => No
[material] =>
[rigidity] =>
)
)
Upvotes: 1
Views: 87
Reputation: 128
You can use multiple arrays. But you'd have to loop through twice. Something like this...
$array = //your array here
//hold the duplicates and values that exist to a name
$dupes = $exists = Array();
//get the duplicates
foreach($array as $nextArray)
{
foreach($nextArray as $key => $val)
{
if(!isset($counts[$key]))
$counts[$key] = Array($val);
else {
//since the key already exists check to see if the value exists
if(in_array($val,$counts[$key])) {
if(!isset($dupes[$key]))
$dupes[$key] = array($val);
}
else {
$counts[$key][] = $val;
}
}
}
}
//apply the duplicates to the array
foreach($array as $arrKey => $nextArray)
{
foreach($nextArray as $key => $val)
{
//if a dupe exist then make the value an empty string
if(isset($dupes[$key]) && in_array($val,$dupes[$key]))
$array[$arrKey][$key] = '';
}
}
Upvotes: 1
Reputation: 7617
You could use the Logic presented in the Commented Code below to delete the Keys whose Values are exactly the same in all the Sub-Arrays. However, The Code below only focuses on 3 Keys namely: material
, rigidity
, and shorten
. If you want to do it for all the Keys, you can simply modify the Code yourself to that effect.
NOTE: All the so-called Duplicates are Deleted... they are not assigned the value of NULL as it wouldn't really make much sense but you can also assign them the value of NULL if you so choose.
$arr = [
33 =>[
'product_id'=> 33,
'name' => 'Aliquam dolor tellus Aliquam dolor tellus',
'color' => 'Brown',
'breat' => 3,
'shorten' => 'Yes',
'material' => 'Natural',
'rigidity' => 'Low',
],
54 =>[
'product_id'=> 54,
'name' => 'New Style',
'color' => 'Black',
'breat' => 4,
'shorten' => 'Yes',
'material' => 'Natural',
'rigidity' => 'Low',
],
23 =>[
'product_id'=> 23,
'name' => 'New Natural',
'color' => 'Yellow',
'breat' => 5,
'shorten' => 'No',
'material' => 'Natural',
'rigidity' => 'Low',
],
];
// CREATE TEMPORAL ARRAYS TO HOLD SCALAR VALUES CORRESPONDING TO
// THE KEYS WHOSE DUPLICITY YOU WISH TO CHECK.
// YOU CAN DO THIS FOR ALL THE KEYS IF YOU CHOOSE
// HERE WE LIMIT THOSE TO THE 3 KEYS BELOW:
$material = $rigidity = $shorten = [];
// LOOP THROUGH YOUR ARRAY AND BUNDLE THE VALUES FOR THE
// `rigidity`, `shorten` & `material` KEYS INTO UNIQUE ARRAYS
// TO BE USED LATER FOR FURTHER PROCESSING *YOUR* DUPLICATES
foreach($arr as $pid=>$data){
// ADD THE VALUES OF THE `rigidity`, `shorten` AND `material` KEYS
// TO THE $rigidity, $shorten AND $material ARRAY RESPECTIVELY:
$material[] = $data['material'];
$rigidity[] = $data['rigidity'];
$shorten[] = $data['shorten'];
}
// EXTRACT THE UNIQUE VALUES OF THE $material, $shorten AND $rigidity ARRAYS
// THE LENGTHS OF WHICH WE SHALL USE TO DETERMINE IF THEY ARE SAME OR NOT
// IF THE LENGTH IS 1; THEN THEY ARE THE SAME ACROSS ALL SUB-ARRAY
// AND THUS ARE FIT TO BE DELETED...
$material = array_unique($material);
$rigidity = array_unique($rigidity);
$shorten = array_unique($shorten);
// LOOP AGAIN THROUGH YOUR ARRAY BUT THIS TIME
// ONLY TO DELETE THE KEYS THAT ARE THE SAME ACROSS ALL SUB-ARRAY
foreach($arr as $pid=>&$data){
if(count($rigidity) == 1){
unset($data['rigidity']);
}
if(count($material) == 1){
unset($data['material']);
}
if(count($shorten) == 1){
unset($data['shorten']);
}
}
// CHECK OUT THE CURRENT VALUE OF THE ARRAY YOU STARTED WITH
var_dump($arr);
THE var_dump($arr) ABOVE YIELDS:
array (size=3)
33 =>
array (size=5)
'product_id' => int 33
'name' => string 'Aliquam dolor tellus Aliquam dolor tellus' (length=41)
'color' => string 'Brown' (length=5)
'breat' => int 3
'shorten' => string 'Yes' (length=3)
54 =>
array (size=5)
'product_id' => int 54
'name' => string 'New Style' (length=9)
'color' => string 'Black' (length=5)
'breat' => int 4
'shorten' => string 'Yes' (length=3)
23 =>
array (size=5)
'product_id' => int 23
'name' => string 'New Natural' (length=11)
'color' => string 'Yellow' (length=6)
'breat' => int 5
'shorten' => string 'No' (length=2)
Upvotes: 1