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は結果です。

タイトルの個別の値をそれぞれカウントし、複数回発生する値のみを返します。

于 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 に答える