svk
svk

Reputation: 4553

get the unique values from the associative array in PHP?

I am having the array like below

Array
(
    [0] => Array
        (
            [catid] => 3
            [parent_id] => 1
            [catname] => Uncategorized
            [catdesc] => 
            [nleft] => 20
            [nright] => 21
            [nlevel] => 1
        )

    [1] => Array
        (
            [catid] => 5
            [parent_id] => 2
            [catname] => Category 2
            [catdesc] => 
            [nleft] => 7
            [nright] => 8
            [nlevel] => 2
        )

    [2] => Array
        (
            [catid] => 3
            [parent_id] => 1
            [catname] => Uncategorized
            [catdesc] => 
            [nleft] => 20
            [nright] => 21
            [nlevel] => 1
        )

    [3] => Array
        (
            [catid] => 3
            [parent_id] => 1
            [catname] => Uncategorized
            [catdesc] => 
            [nleft] => 20
            [nright] => 21
            [nlevel] => 1
        )

    [4] => Array
        (
            [catid] => 7
            [parent_id] => 2
            [catname] => Flower
            [catdesc] => 
            [nleft] => 11
            [nright] => 18
            [nlevel] => 2
        )

    [5] => Array
        (
            [catid] => 8
            [parent_id] => 7
            [catname] => Lillies
            [catdesc] => 
            [nleft] => 12
            [nright] => 13
            [nlevel] => 3
        )

    [6] => Array
        (
            [catid] => 10
            [parent_id] => 7
            [catname] => Jasmine
            [catdesc] => 
            [nleft] => 16
            [nright] => 17
            [nlevel] => 3
        )

    [7] => Array
        (
            [catid] => 7
            [parent_id] => 2
            [catname] => Flower
            [catdesc] => 
            [nleft] => 11
            [nright] => 18
            [nlevel] => 2
        )

    [8] => Array
        (
            [catid] => 8
            [parent_id] => 7
            [catname] => Lillies
            [catdesc] => 
            [nleft] => 12
            [nright] => 13
            [nlevel] => 3
        )

    [9] => Array
        (
            [catid] => 10
            [parent_id] => 7
            [catname] => Jasmine
            [catdesc] => 
            [nleft] => 16
            [nright] => 17
            [nlevel] => 3
        )

    [10] => Array
        (
            [catid] => 3
            [parent_id] => 1
            [catname] => Uncategorized
            [catdesc] => 
            [nleft] => 20
            [nright] => 21
            [nlevel] => 1
        )

    [11] => Array
        (
            [catid] => 3
            [parent_id] => 1
            [catname] => Uncategorized
            [catdesc] => 
            [nleft] => 20
            [nright] => 21
            [nlevel] => 1
        )

    [12] => Array
        (
            [catid] => 10
            [parent_id] => 7
            [catname] => Jasmine
            [catdesc] => 
            [nleft] => 16
            [nright] => 17
            [nlevel] => 3
        )

    [13] => Array
        (
            [catid] => 3
            [parent_id] => 1
            [catname] => Uncategorized
            [catdesc] => 
            [nleft] => 20
            [nright] => 21
            [nlevel] => 1
        )

    [14] => Array
        (
            [catid] => 8
            [parent_id] => 7
            [catname] => Lillies
            [catdesc] => 
            [nleft] => 12
            [nright] => 13
            [nlevel] => 3
        )

)

From this array I want to get the Unique Arrays.ie. If catId is same it needs to eliminate from the list. How can I do this?

Upvotes: 3

Views: 7913

Answers (4)

lonesomeday
lonesomeday

Reputation: 237865

Presuming you're using PHP >5.3, you could use array_filter:

$catIds = array();
$myarray = array_filter($myarray, function($el) use (&$catIds) {
    if (in_array($el['catid'], $catIds)) { // if the id has already been seen
        return false; // remove it
    } else {
        $catIds[] = $el['catid']; // the id has now been seen
        return true; // but keep the first occurrence of it
    }
});

Upvotes: 2

Jimesh Gajera
Jimesh Gajera

Reputation: 612

You can use this for Convert Multidimentional array to Single array.

$out = array();
for($i=0;$i<count($rows);$i++) {
    $out = $rows[$i];
}

And you can check this by

print_r($out);

Upvotes: 0

Berry Langerak
Berry Langerak

Reputation: 18859

Where do you get that array from (presuming it doesn't magically appear)? If you're getting those results from a database, wouldn't it be much easier to limit them there? E.g.

SELECT DISTINCT cat_id, parent_id, catname FROM yourtable;

Upvotes: 0

James C
James C

Reputation: 14149

If you're using an older version:

$out = array();
foreach ($arr as $row) {
    $out[$row['catid']] = $row;
}
$array = array_values($out); // only required if you mind the new array being assoc

Upvotes: 11

Related Questions