0

$a = ['Ava', 'Emma', 'Olivia']; $b = ['Olivia', 'Sophia', 'Emma'];

I want the output to be ['Emma', 'Olivia', 'Ava', 'Sophia'] in any particular order without using array functions.

This is what i tried

<?php
//function unique_names($a,$b){
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$z= $a;
$c = count($b);
$d = count($a);
//loop for b
    $e = 0;
for($i=0;$i<$c;$i++){ //b
    
    for($j=0;$j<$d;$j++){
            
        if($b[$i] != $a[$j]){
        $z[$d+1] = $b[$i];
        break;
        }else{
            //$z[$e] = $a[$j];
        }
  
    }
    
 
        
    
}
echo"<pre>ans";print_r($z);
die;
//return $z;
//}


//echo"<pre>ans"; print_r(unique_names($a,$b));
?>

Also i made it work using in_array but was later told that even that function is not allowed.

<?php
function unique_names($a,$b){
$z= $a;
$c = count($b);
$d = count($a);
for($i=0;$i<$c;$i++){
    
  if(! in_array($b[$i], $a)){
            $z[$d+1] = $b[$i];
  }
        
    
}
return $z;
}
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];

print_r(unique_names($a,$b));
?>
  • Does this answer your question? [PHP merge arrays with only NOT DUPLICATED values](https://stackoverflow.com/questions/10572546/php-merge-arrays-with-only-not-duplicated-values) – nbk May 13 '21 at 15:26
  • 2
    _"...without using array functions"_ sounds like an assignment. So, what have you tried? Where did you get stuck? Please note that while we're glad to help with any issues you encounter, you are still expected to make an honest effort at solving this task yourself. – El_Vanja May 13 '21 at 15:28
  • I have made the changes in question and have added what i tried. Thanks – Parin Nagda May 13 '21 at 15:47
  • what about `array_unique(array_merge($a, $b));` – Kinglish May 13 '21 at 15:58
  • No array functions are allowed. – Parin Nagda May 13 '21 at 15:58
  • My first piece of advice would be to swap `for` loops with `foreach` loops. That way you can ensure that you iterate every element of one array for every element of the other, without potentially getting yourself tangled in index values. – El_Vanja May 13 '21 at 16:02
  • My second advice is to familiarize yourself with the [basics of debugging](http://www.phpknowhow.com/basics/basic-debugging/). If you dumped your resulting array (`$z`) inside the `if` condition, you would be able to see that you are just adding another element at the end and overwriting it in the following iteration. – El_Vanja May 13 '21 at 16:05
  • How about `$a = ['a','b','b'] , $b = ['b','b','b','c']`? What needs to be the output? – nice_dev May 13 '21 at 16:08
  • The main flaw of your logic is that you compare the current element of the outer loop to the element of the inner loop: `if($b[$i] != $a[$j])` and then immediately take action if they're different. Just because `$b[$i]` (for example, Olivia) is different than `$a[$j]` (for example, Ava) doesn't mean you should immediately include it in the result. You need to compare it to *all* the elements of the other array before you decide to include it. – El_Vanja May 13 '21 at 16:11
  • To close voters, please retract your close votes. They don't make sense anymore. – nice_dev May 13 '21 at 16:13
  • 1
    @nice_dev The output needs to be ['a' ,'b' , 'c'] – Parin Nagda May 13 '21 at 17:11
  • @ParinNagda Great. In that case, you have a working solution below. – nice_dev May 13 '21 at 18:17

1 Answers1

2

You can use next code:

<?php
$a = ['Ava', 'Emma', 'Olivia']; 
$b = ['Olivia', 'Sophia', 'Emma'];

$values = [];

// Add values from first array
foreach($a as $v) {
    $values[$v] = true;
}

// Add values from second array
// all exists names will be overwrited
// new values will be addded
foreach($b as $v) {
    $values[$v] = true;
}

// Transform keys to plain result
foreach($values as $key=>$val) {
    $result[] = $key;
}
var_dump($result);

Execute PHP online

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39