1

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
        )
)
2
  • So basically what you want is to print only items that have the same title ? Commented Sep 21, 2012 at 21:40
  • Yes, this might be a super easy task, but my brain seems to be mush right now. I also need to keep the original keys in tact. Commented Sep 21, 2012 at 21:43

3 Answers 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; });

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

)

Comments

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

5 Comments

You're not answering the question.
@alfasin Yes I am. Unless you want me to explain how foreach works.
As of my understanding, OP would like to search for duplicate titles in his array.
Title 2 was just an example. Title values are likely to change. So, this would need to be able to search dynamically for any matches on that key.
@MAZUMA Oh, I misunderstood you then. I though you needed a way to find all subarrays with a specific key matching an arbitary value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.