Home >Backend Development >PHP Tutorial >php二维数组去除重合数据

php二维数组去除重合数据

WBOY
WBOYOriginal
2016-06-23 14:14:461296browse

PHP 二维数组

Array
(
    [0] => Array
        (
            [knowledge_id] => 1
            [tag_id] => 2
        )

    [1] => Array
        (
            [knowledge_id] => 1
            [tag_id] => 3
        )

)

把他里面量相同的[knowledge_id] => 1合并只留一个??????

回复讨论(解决方案)

留哪个呢?还是2个 tag_id 都要?

不用只要一个数组[0] => Array
        (
            [knowledge_id] => 1
            [tag_id] => 2
        )

设原数组为 $ar, 则

$res = array(); //结果数组foreach($ar as $v) {  if(! isset($res[$v['knowledge_id']])) $res[$v['knowledge_id']] = $v;}$res = array_values($res);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn