0

What would be the best way to ignore the duplicates for customer_name in this grouped array? I only want to display a list of unique names.

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [model_id] => 1
                    [work_order] => 10999
                    [id] => 1
                    [model_name] => STC1.5
                    [stock] => 3
                    [free] => 1
                    [customer_name] => Jane Doe
                )

            [1] => Array
                (
                    [model_id] => 1
                    [work_order] => 10998
                    [id] => 1
                    [model_name] => STC1.5
                    [stock] => 3
                    [free] => 1
                    [customer_name] => Jane Doe
                )

        )
)
  • have you tried array_unique()? this should work for you `array_unique($array, SORT_REGULAR);` – A l w a y s S u n n y Jul 30 '18 at 16:16
  • Possible duplicate of [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) – Script47 Jul 30 '18 at 16:18
  • 4
    Which record should be ignored, `10998` or `10999`? – MonkeyZeus Jul 30 '18 at 16:20
  • 2
    The question is not clear. Do you need to get only the list of `customer_name`s without duplicates or you need to remove some entries of the array to avoid having two entries with the same `customer_name`? If the latter case is the correct one then you should define the rules of ignoring the duplicates (apart from them having the same `customer_name`). What entry to keep when a duplicate is detected? The first one? The last one? The one that have the smallest/biggest value in `id`? etc. – axiac Jul 30 '18 at 16:22
  • I need to display a list of all customer names unless they are duplicated. It doesn't matter which is displayed as long as all the names are unique. – user3616336 Jul 30 '18 at 16:25
  • Would `Jane Doe` and a misspelled `jane Doe` be two people? – MonkeyZeus Jul 31 '18 at 11:34

2 Answers2

0

Assuming that you ONLY care about unique names and $arr is:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [model_id] => 1
                    [work_order] => 10999
                    [id] => 1
                    [model_name] => STC1.5
                    [stock] => 3
                    [free] => 1
                    [customer_name] => Jane Doe
                )

            [1] => Array
                (
                    [model_id] => 1
                    [work_order] => 10998
                    [id] => 1
                    [model_name] => STC1.5
                    [stock] => 3
                    [free] => 1
                    [customer_name] => Jane Doe
                )

        )
)

then you can use:

$unique_names = array_keys(array_flip(array_column($arr[1],'customer_name')));

If you are concerned about micro-optimization then try:

$unique_names = array();

foreach( $arr[1] as $v )
{
    $unique_names[$v['customer_name']] = $v['customer_name'];
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
0

You can create an array with only the name with array_column and use array_unique on that.
Then use array_intersect_key to get the corresponding subarrays from the main array.

$name = array_unique(array_column($arr[1], "customer_name"));
$unique = array_intersect_key($arr, $name);
Andreas
  • 23,610
  • 6
  • 30
  • 62