1

I have an array not too different from the below (with a lot more sub-arrays). There are any number of sub-arrays and any level of arrays below them. It's only ever this many levels deep however.

Let's say I wanted to bring back all unique pets (in [1]) how would go about doing that? Similarly, if I wanted all unique colours (in [2]) or all unique cars [in [3]]

I tried solutions from How to remove duplicate values from a multi-dimensional array in PHP and other similar SO pages but got nowhere fast and now back to a blank page

Any advice?

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Jo Bloggs
                    [1] => Cat
                    [2] => Red
                    [3] => Nissan
                )
            [1] => Array
                (
                    [0] => Patrick
                    [1] => Dog
                    [2] => Blue
                    [3] => Nissan
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => Charloe
                    [1] => Moose
                    [2] => Green
                    [3] => Ford
                )
            [1] => Array
                (
                    [0] => Patrick
                    [1] => Dog
                    [2] => Blue
                    [3] => Porsche
                )
        )
...
pee2pee
  • 3,619
  • 7
  • 52
  • 133

1 Answers1

3

PHP offers a wide range of convenience functions to handle array. I strongly suggest that you dive into the official documentation and start getting creative yourself: http://php.net/manual/en/ref.array.php

Here is a simple example of the actual algorithm you asked using two such functions:

<?php  
$input = [
    [
        [
            0 => "Jo Bloggs",
            1 => "Cat",
            2 => "Red",
            3 => "Nissan"
        ],
        [
            0 => "Patrick",
            1 => "Dog",
            2 => "Blue",
            3 => "Nissan"
        ],
    ],
    [
        [
            0 => "Charloe",
            1 => "Moose",
            2 => "Green",
            3 => "Ford"
        ],
        [
            0 => "Patrick",
            1 => "Dog",
            2 => "Blue",
            3 => "Porsche"
        ]
    ]
];

$aspect = 1;
$output = [];
array_walk_recursive($input, function($value, $key) use ($aspect, &$output) {
    if ($key == $aspect) {
        $output[] = $value;
    }
});

print_r(array_unique($output));

The output of above code obviously is:

Array
(
    [0] => Cat
    [1] => Dog
    [2] => Moose
)

What type of elements are filtered out can be controlled via the $aspect variable, it holds the keys you want to identify matches with.

Obviously there are other approaches to what you asked, this is just a simple example. One might also prefer to accept only unique values in the $output array, for example, that would save the final call to array_unique()...

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Amazing - thank you - I work/learn best off examples but will do what you suggested and delve in – pee2pee Dec 14 '18 at 20:16
  • Sure, it always appears straight forward if you look at working solutions. But you will only really learn to master a language if you start trying around yourself. You need to explore the field. And by explaining the algorithms to yourself you will learn much faster compared to looking at code others wrote. – arkascha Dec 14 '18 at 20:17
  • You've certainly been more helpful than those that just want to close a question - much appreciated – pee2pee Dec 14 '18 at 20:18
  • Glad I could be of help. Have an interesting journey diving deeper into programming ;-) – arkascha Dec 14 '18 at 20:23