-1

Within an array, how can I remove records with a repeated value? For example, I have:

container_array
(
    [0] => sub_array
        (
            [order_id] => 14539972
            [name] => Desiree
        )

    [1] => sub_array
        (
            [order_id] => 14570621
            [name] => Jana
        )

    [2] => sub_array
        (
            [order_id] => 14574112
            [name] => Debra
        )

    [3] => sub_array
        (
            [order_id] => 14570621
            [name] => Jana
        )

    [4] => sub_array
        (
            [order_id] => 14570621
            [name] => Jana
        )
)

Order ID 14570621 is repeated multiple times. How can I remove the duplicates so that only one remains? The resulting array should be:

container_array
(
    [0] => sub_array
        (
            [order_id] => 14539972
            [name] => Desiree
        )

    [1] => sub_array
        (
            [order_id] => 14570621
            [name] => Jana
        )

    [2] => sub_array
        (
            [order_id] => 14574112
            [name] => Debra
        )
)
GTS Joe
  • 3,612
  • 12
  • 52
  • 94
  • 1
    Does this answer your question? [How to remove duplicate values from a multi-dimensional array in PHP](https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php). I would suggest [this answer](https://stackoverflow.com/a/18373723/965834) instead of the accepted one, btw. – Jeto Jan 24 '20 at 15:25
  • Yes, Jack's answer was my solution. Thank you. :) – GTS Joe Jan 24 '20 at 15:44

1 Answers1

0

This code can help you?

<?php
$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15),
  array("Land Rover",17,15)
  );

var_dump($cars);
$input = array_map("unserialize", array_unique(array_map("serialize", $cars)));
echo '<br>';
var_dump($input);


?>
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34