I have a nested multi dimensional array - each array has a nested array called values which contains a array
it looks like this:
Array
(
[0] => Array
(
[id] => 53
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 0
[name] => group 1
[description] =>
[parent_option_type] => single
[price_type] => not_set
[price_sell] =>
[price_purchase] =>
[sort] => 1
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
[values] => Array
(
[0] => Array
(
[id] => 54
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 53
[name] => option name
[description] =>
[parent_option_type] => not_set
[price_type] => free
[price_sell] => 0.00
[price_purchase] => 0.00
[sort] => 1
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
)
[1] => Array
(
[id] => 55
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 53
[name] => option name
[description] =>
[parent_option_type] => not_set
[price_type] => free
[price_sell] => 0.00
[price_purchase] => 0.00
[sort] => 2
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
)
[2] => Array
(
[id] => 56
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 53
[name] => option name
[description] =>
[parent_option_type] => not_set
[price_type] => free
[price_sell] => 0.00
[price_purchase] => 0.00
[sort] => 3
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
)
)
)
[1] => Array
(
[id] => 57
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 0
[name] => group 1
[description] =>
[parent_option_type] => single
[price_type] => not_set
[price_sell] =>
[price_purchase] =>
[sort] => 2
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
[values] => Array
(
[0] => Array
(
[id] => 58
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 57
[name] => option name
[description] =>
[parent_option_type] => not_set
[price_type] => free
[price_sell] => 0.00
[price_purchase] => 0.00
[sort] => 1
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
)
[1] => Array
(
[id] => 59
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 57
[name] => option name
[description] =>
[parent_option_type] => not_set
[price_type] => free
[price_sell] => 0.00
[price_purchase] => 0.00
[sort] => 2
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
)
[2] => Array
(
[id] => 60
[delivery_partner_id] => 6
[delivery_partner_product_id] => 37
[parent_id] => 57
[name] => option name
[description] =>
[parent_option_type] => not_set
[price_type] => free
[price_sell] => 0.00
[price_purchase] => 0.00
[sort] => 3
[created_at] => 2018-08-12 21:35:14
[updated_at] => 2018-08-12 21:35:14
)
)
)
)
1
The problem is that it is possible to have same named groups with exactly the same values. But how can I make this unique based on the group values and the "values"?
I tried with array unique but the problem is the nested array with values from some reason this will not work. I'm trying to find another approach to do this.
It should check first if two groups exists with the same values, if that is true it should check the values array and check if both the values are the same. If all is the same, it should remove the duplicate.
Tried this, seems to work, altough you might wan't to rewrite it to something more efficient :
function removeDupplicates($array) {
$result = [];
while(count($array) > 0) {
$currentRecord = array_pop($array);
$valueFound = false;
foreach($array as $value) {
if ($value === $currentRecord) {
$valueFound = true;
}
}
if (!$valueFound) {
$result[] = $currentRecord;
}
}
return $result;
}