从PHP中的关联数组中获取唯一值?

时间:2011-05-17 11:51:33

标签: php multidimensional-array

我正在使用下面的数组

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
        )

)

从这个数组我想得到Unique Arrays.ie。如果catId相同,则需要从列表中删除。 我怎么能这样做?

4 个答案:

答案 0 :(得分:11)

如果您使用的是旧版本:

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

答案 1 :(得分:2)

假设你使用PHP> 5.3,你可以使用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
    }
});

答案 2 :(得分:0)

你从哪里得到那个阵列(假设它没有神奇地出现)?如果您从数据库中获取这些结果,那么 更容易将它们限制在那里吗?例如。

SELECT DISTINCT cat_id, parent_id, catname FROM yourtable;

答案 3 :(得分:0)

您可以将此用于将多维数组转换为单个数组。

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

您可以通过

查看
print_r($out);