There is an array with obekatmi (goods from the database):

array(10) { [0]=> object(stdClass)#37 (6) { ["id"]=> string(5) "31739" ["pro_title"]=> string(48) "Π‘Π»ΡƒΠ·Π° с Π΄Π΅ΠΊΠΎΡ€ΠΎΠΌ ΠΈΠ· ΠΏΠ°ΠΉΠ΅Ρ‚ΠΎΠΊ" ["price"]=> string(9) "154100.00" ["parent_id"]=> string(5) "31739" ["photo_id"]=> string(6) "116489" ["value"]=> string(7) "Rodarte" } [1]=> object(stdClass)#38 (6) { ["id"]=> string(5) "31739" ["pro_title"]=> string(48) "Π‘Π»ΡƒΠ·Π° с Π΄Π΅ΠΊΠΎΡ€ΠΎΠΌ ΠΈΠ· ΠΏΠ°ΠΉΠ΅Ρ‚ΠΎΠΊ" ["price"]=> string(9) "154100.00" ["parent_id"]=> string(5) "31739" ["photo_id"]=> string(6) "116489" ["value"]=> string(7) "Rodarte" } [2]=> object(stdClass)#39 (6) { ["id"]=> string(5) "31739" ["pro_title"]=> string(48) "Π‘Π»ΡƒΠ·Π° с Π΄Π΅ΠΊΠΎΡ€ΠΎΠΌ ΠΈΠ· ΠΏΠ°ΠΉΠ΅Ρ‚ΠΎΠΊ" ["price"]=> string(9) "154100.00" ["parent_id"]=> string(5) "31739" ["photo_id"]=> string(6) "116489" ["value"]=> string(7) "Rodarte" } [3]=> object(stdClass)#40 (6) { ["id"]=> string(5) "31762" ["pro_title"]=> string(30) "Π‘Π²ΠΈΡ‚Π΅Ρ€ ΠΈΠ· ΡˆΠ΅Ρ€ΡΡ‚ΠΈ" ["price"]=> string(8) "39370.00" ["parent_id"]=> string(5) "31762" ["photo_id"]=> string(6) "116609" ["value"]=> string(5) "MONSE" } [4]=> object(stdClass)#41 (6) { ["id"]=> string(5) "31762" ["pro_title"]=> string(30) "Π‘Π²ΠΈΡ‚Π΅Ρ€ ΠΈΠ· ΡˆΠ΅Ρ€ΡΡ‚ΠΈ" ["price"]=> string(8) "39370.00" ["parent_id"]=> string(5) "31762" ["photo_id"]=> string(6) "116609" ["value"]=> string(5) "MONSE" } [5]=> object(stdClass)#42 (6) { ["id"]=> string(5) "31762" ["pro_title"]=> string(30) "Π‘Π²ΠΈΡ‚Π΅Ρ€ ΠΈΠ· ΡˆΠ΅Ρ€ΡΡ‚ΠΈ" ["price"]=> string(8) "39370.00" ["parent_id"]=> string(5) "31762" ["photo_id"]=> string(6) "116609" ["value"]=> string(5) "MONSE" } [6]=> object(stdClass)#43 (6) { ["id"]=> string(5) "31762" ["pro_title"]=> string(30) "Π‘Π²ΠΈΡ‚Π΅Ρ€ ΠΈΠ· ΡˆΠ΅Ρ€ΡΡ‚ΠΈ" ["price"]=> string(8) "39370.00" ["parent_id"]=> string(5) "31762" ["photo_id"]=> string(6) "116609" ["value"]=> string(5) "MONSE" } [7]=> object(stdClass)#44 (6) { ["id"]=> string(5) "31765" ["pro_title"]=> string(25) "Π‘Π΅Ρ€ΡŒΠ³ΠΈ Gold Baroque" ["price"]=> string(8) "50710.00" ["parent_id"]=> string(5) "31765" ["photo_id"]=> string(6) "116492" ["value"]=> string(7) "Rodarte" } [8]=> object(stdClass)#45 (6) { ["id"]=> string(5) "31768" ["pro_title"]=> string(40) "Π’ΠΎΠΏ ΠΈΠ· вискозы ΠΈ шСлка" ["price"]=> string(8) "90970.00" ["parent_id"]=> string(5) "31768" ["photo_id"]=> string(6) "116490" ["value"]=> string(7) "Rodarte" } [9]=> object(stdClass)#46 (6) { ["id"]=> string(5) "31768" ["pro_title"]=> string(40) "Π’ΠΎΠΏ ΠΈΠ· вискозы ΠΈ шСлка" ["price"]=> string(8) "90970.00" ["parent_id"]=> string(5) "31768" ["photo_id"]=> string(6) "116490" ["value"]=> string(7) "Rodarte" } } 

How to remove duplicate objects by key parent_id?

I tried array_unique, but not that. Thanks for the help.

  • What does it mean by the parent_id key parent_id remove duplicates among each group with the same parent_id , or even remove all duplicate objects with the same parent_id ? - teran
  • Is the order of the elements in the array important? If after the removal of duplicates the order is broken, will it be critical? - Anton Shchyrov
  • remove all duplicate objects with the same parent_id leaving only one. thank. - Nikita Ryazanov
  • @ anton-shchyrov order is not critical - Nikita Ryazanov

2 answers 2

something like this?

 $result = []; foreach($data as $o) { $result[$o->parent_id] = $o; } //$result = array_values($result); print_r($result); 

use an associative array, and the keys are parent_id values. Each subsequent object will replace the previous one. As a result, get no duplicates. If you need to return consecutive numeric keys, use array_values()

  • Yes! exactly what is needed. thank. I will know) - Nikita Ryazanov

If the order of the elements in the resulting array is not important, then you need to sort the source array by the parent_id field, and then delete all duplicates in the loop

 $data = [......]; usort($data, function($a, $b) { if ($a->parent_id < $b->parent_id) return -1; else if ($a->parent_id == $b->parent_id) return 0; else return 1; }); $new_data = array_filter( $data, function($val, $key) { return ($key == 0) || ($data[$key - 1]->partent_id != $val->parent_id); }, ARRAY_FILTER_USE_BOTH ); 

If the order is important, then we first count the number of elements with different parent_id , and then delete the extra

 $data = [..........]; $counts = []; foreach ($data as $val) { if (!isset($counts[$val->parent_id]) $counts[$val->parent_id] = 0; else $counts[$val->parent_id]++; }; $new_data = array_filter( $data, function($val, $key) { if ($counts[$val->parent_id] === 0) { return true; $counts[$val->parent_id]--; return false; }, ARRAY_FILTER_USE_BOTH );