PHP - Pull out array keys that have matching values?

Is there a way to look inside an array and pull out the keys that have keys with matching values? Questions like this have been asked, but I'm not finding anything conclusive.

So if my array looks like this

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] => 
)

And I want to get this

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


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

Answers


$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; });

where $input is the original array and $output is the result.

It counts each distinct value of title and returns only those that occur more than once.


@Bugs's answer is better than this one, but I'm still posting it since I got the feeling that my solution will be easier to understand:

$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);

Output:

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

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

)

foreach($array as &$subarray) {
    if(is_array($subarray) && isset($subarray['title']) && $subarray['title'] == 'Title 2') {
        $matches[] = $subarray;
    }
}

Can be easily wrapped in a function taking subarray key and value as arguments.


Need Your Help

Type-safe formatted output in Scala

scala printf

I am thinking if it is possible to implement a type-safe printf in Scala. Suppose we need to support only string literals, %s, and %d

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.