1

Implement the find_common function. When passed two arrays of names, it will return an array containing the names that appear in either or both arrays. The returned array should have no duplicates.

<?php
class Names
{
 public static function find_common($array1, $array2)
 {
    $arr = array();
    foreach($array1 as $value1){
        if(in_array($value1,$array2)){
            array_push($arr,$value1);
        }
    }

    return $arr;
  }
}

$names = Names::find_common(['Ava', 'Emma', 'Olivia'], ['Olivia', 'Sophia', 'Emma']);
echo join(', ', $names); // should print Emma, Olivia, Ava, Sophia

OUTPUT SHOUD PRINT: Emma, Olivia, Ava, Sophia What I am getting is only the Emma, Olivia :(

3
  • 1
    An actual question would be nice. Commented Sep 26, 2018 at 10:12
  • Just refer to the output. It should output : Emma, Olivia, Ava, Sophia Commented Sep 26, 2018 at 10:14
  • Can you add find_common as well ? Commented Sep 26, 2018 at 10:32

4 Answers 4

3

Use

array_unique(array_merge(['Ava', 'Emma', 'Olivia'], ['Olivia', 'Sophia', 'Emma']));

array_merge will put all values in a single array then array_unique wil remove duplicates.

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

5 Comments

That's It!!!!! Thank you sir. Maybe you can help me more with my problem?
What problem do you still have ? Can't the answer be accepted ?
After 2 mins I can accept your answer! Can you pls give me your contact? I have many problems :(
Thanks but I won't have time to go through all your problems privately. But I'm not the only one who can help you here, don't hesitate to ask more questions !
One more problem. Pls give me your contact
1

This is real easy. Use array_intersect():

array_intersect(['Ava', 'Emma', 'Olivia'], ['Olivia', 'Sophia', 'Emma']);

Array will contain Emma & Olivia.

Check the docs! :-) http://php.net/manual/en/function.array-intersect.php

UPDATE Your question doesn't reflect your desired output. Do you really mean to ask how to merge arrays without duplicates?

If that's the case, merge the array with

array_unique(array_merge(['Ava', 'Emma', 'Olivia'], ['Olivia', 'Sophia', 'Emma']));

http://php.net/manual/en/function.array-merge.php

http://php.net/manual/en/function.array-unique.php

3 Comments

Your output is only Emma and olivia. Output should be Emma, Olivia, Ava, Sophia
He said the output should print Emma, Olivia, Ava, Sophia ;)
Yup so he needs a merge then. Updating
0

Try This works for me:

function unique_names(array $array1, array $array2) : array
    { 
     $array3= array_intersect($array1,$array2);
     return array_unique(array_merge($array3,$array1, $array2)); 
    }

Comments

0
$names = unique_names(['Ava', 'Emma', 'Olivia'], ['Olivia', 'Sophia', 'Emma']);
echo join(', ', $names); // should print Emma, Olivia, Ava, Sophia

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term 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.