Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter 2 dimensional array

Tags:

arrays

php

I have a array that looks a bit like this

array(
    [0] => array(
        ['id'] => 29
        ['name'] => john
        )
    [1] => array(
        ['id'] => 30
        ['name'] => joe
        ) 
    [2] => array(
        ['id'] => 29
        ['name'] => jake
        ) 
)

And that goes on for a while.

I've found the question elsewhere (here) and (here) But neither works.

With the first one I get the following array

array(
    [0] => 29
    [1] => jake
)

And with te second one it only filters out exact duplicates and not duplicates with jus the same id.

I want all the duplicates with the same id removed from the array, how do I do that?

like image 468
Liam de Haas Avatar asked Dec 20 '22 02:12

Liam de Haas


1 Answers

Simple with PHP >= 5.5.0:

$result = array_column($array, null, 'id');

Optionally:

$result = array_values(array_column($array, null, 'id'));
like image 124
AbraCadaver Avatar answered Jan 02 '23 12:01

AbraCadaver