MAZUMA
MAZUMA

Reputation: 943

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

Upvotes: 1

Views: 366

Answers (3)

Nir Alfasi
Nir Alfasi

Reputation: 53525

@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
        )

)

Upvotes: 0

Bugs
Bugs

Reputation: 1452

$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.

Upvotes: 2

lafor
lafor

Reputation: 12776

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.

Upvotes: -1

Related Questions