I will mention my array bellow. I need to remove duplicating keys from array but also I need to keep other elements merged. I know this is not clean enough to understand so I will explain.
This the expected result I want.
Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1
Categories 1 --> Sub Cat Items 2
Categories 2 --> Sub Cat Items 3
Categories 3 --> Sub Cat Items 4
Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5
but from the query I am getting some thing like this.
Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1
Main Cat Items 1 -> Categories 1 --> Sub Cat Items 2
Main Cat Items 1 -> Categories 2 --> Sub Cat Items 3
Main Cat Items 1 -> Categories 3 --> Sub Cat Items 4
Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5
My query can't refine to get the result. so I have to do it by php. this is what I had tried.
$input = array_map("unserialize", array_unique(array_map("serialize", $test)));
using this nothing changed I got the same array with duplicates.
$serialized = array_map('serialize', $test);
$unique = array_unique($serialized);
$tdsdsdf = array_intersect_key($test, $unique);
this one also the same.
$unique = array();
foreach ($test as $key => $value) {
if (!array_key_exists($value['main_cat_id'], $unique)) {
$unique[$value['main_cat_id']] = $value;
}
}
when trying with this it is removing the all duplicates with all the elements.
this is the array I am getting just after the db query.
Array
(
[0] => Array
(
[main_cat_id] => 1
[main_cat_name] => main cat 1
[cat_id] => 1
[cat_name] => cat1 parent 1
[sub_cat_name] => sub 1 cat1 parent 1
)
[1] => Array
(
[main_cat_id] => 1
[main_cat_name] => main cat 1
[cat_id] => 1
[cat_name] => cat1 parent 1
[sub_cat_name] => sub 2 cat1 parent 1
)
[2] => Array
(
[main_cat_id] => 1
[main_cat_name] => main cat 1
[cat_id] => 4
[cat_name] => cat2 parent 1
[sub_cat_name] => sub 3 cat 4 parent 1
)
[3] => Array
(
[main_cat_id] => 1
[main_cat_name] => main cat 1
[cat_id] => 5
[cat_name] => cat3 parent 1
[sub_cat_name] =>
)
[4] => Array
(
[main_cat_id] => 2
[main_cat_name] => main cat 2
[cat_id] => 6
[cat_name] => cat 4 parent 2
[sub_cat_name] =>
)
[5] => Array
(
[main_cat_id] => 2
[main_cat_name] => main cat 2
[cat_id] => 7
[cat_name] => cat 5 parent 2
[sub_cat_name] =>
)
[6] => Array
(
[main_cat_id] => 3
[main_cat_name] => main cat 3
[cat_id] => 8
[cat_name] => cat 6 parent 3
[sub_cat_name] =>
)
)