1

有没有办法查看数组内部并取出具有匹配值的键的键?有人问过这样的问题,但我没有找到任何结论性的东西。

所以如果我的数组看起来像这样

Array
(
    [0] => Array
        (
            [title] => Title 1
            [type] => 
            [message] => 
        )

    [1] => Array
        (
            [title] => Title 2
            [type] => 
            [message] => 
        )

    [2] => Array
        (
            [title] => Title 3
            [type] => 
            [message] => 
        )

    [3] => Array
        (
            [title] => Title 2
            [type] => Limited
            [message] => 39
        )

    [4] => Array
        (
            [title] => Title 4
            [type] => Offline
            [message] => 41
        )

    [5] => Array
        (
            [title] => Title 5
            [type] => 
            [message] => 
)

我想得到这个

Array
(
    [1] => Array
        (
            [title] => Title 2
            [type] => 
            [message] => 
        )


    [3] => Array
        (
            [title] => Title 2
            [type] => Limited
            [message] => 39
        )
)
4

3 回答 3

2
$titlesCount = array_count_values(array_map(function($item){ return $item['title']; }, $input));
$output = array_filter($input, function($item) use(&$titlesCount){ return $titlesCount[$item['title']] > 1; });

其中 $input 是原始数组, $output 是结果。

它计算每个不同的 title 值,并仅返回那些多次出现的值。

于 2012-09-21T22:02:40.060 回答
0

@Bugs 的答案比这个更好,但我仍然发布它,因为我觉得我的解决方案会更容易理解:

$arr = Array
(
    Array
        (
            "title" => "Title 1",
            "type" => NULL,
            "message" => NULL
        ),
    Array
        (
            "title" => "Title 2",
            "type" => NULL,
            "message" => NULL
        ),
    Array
        (
            "title" => "Title 3",
            "type" => NULL,
            "message" => NULL
        ),
    Array
        (
            "title" => "Title 2",
            "type" => "Limited",
            "message" => 39
        ),
    Array
        (
            "title" => "Title 4",
            "type" => "Offline",
            "message" => 41
        ),
    Array
        (
            "title" => "Title 5",
            "type" => NULL,
            "message" => NULL
        )
);

//create a "map" that will hold all the values of previous titles
$map = array();
$res = array();
foreach ($arr as $key => $subarr) {
    foreach ($subarr as $subkey => $value) {           
        if($subkey === "title"){            
            if($map[$value]){ //if such a title already exists, add both of them to the result                
                $res[] = $arr[$map[$value]];
                $res[] = $arr[$key];
            }
            else { // add the current into the map                
                $map[$value] = $key;
            }
        }
    }
}
print_r($res);

输出:

Array
(
    [0] => Array
        (
            [title] => Title 2
            [type] => 
            [message] => 
        )

    [1] => Array
        (
            [title] => Title 2
            [type] => Limited
            [message] => 39
        )

)
于 2012-09-21T22:17:50.533 回答
-1
foreach($array as &$subarray) {
    if(is_array($subarray) && isset($subarray['title']) && $subarray['title'] == 'Title 2') {
        $matches[] = $subarray;
    }
}

可以很容易地包装在一个以子数组键和值作为参数的函数中。

于 2012-09-21T21:41:26.427 回答