1

Got an problem while doing my task.

I have two arrays, first array show random numbers from 20 to 60. Second array show numbers from 40 to 80.

I need to create a third array which is going to be a merge of two arrays but without duplicates.

My code looks like this:

$myLength=mt_rand(50,80);
$array1=array();
for ($i=0;$i<$myLength;$i++){
    $array1[$i]=mt_rand(20,60);
}
print_r($array1);
echo "<br>";
$array2=array();
for ($i=0;$i<$myLength;$i++){
    $array2[$i]=mt_rand(40,80);
}
print_r($array2);
echo "<br>";
$array3 = array_merge($array1,$array2);
if ($array2!=$array1) {
    print_r($array3);
}

I probably understand that I can fix that with something like

if(arr1.value != arr2.value) { print value to arr3 }

But I don't know how to implement it. Or maybe there is even easier option to do it, but I stuck and just trying to find a way of doing it. Maybe there is already ready function for this task?

Any ideas how I can make it most precise way?

1
  • Does the final array need to be a fixed length, or can it be variable? Please add concrete requirements otherwise you'll receive answers that don't do what you need them to do. Commented Mar 16, 2021 at 15:36

3 Answers 3

3

$array3 = array_unique(array_merge($array1, $array2));

array_merge merges the two arrays while array_unique makes sure that there are no duplicates in an array.

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

2 Comments

Please add some explanation of how your code would solve the problem.
@NigelRen is it better now?
0

try it

function arrayRecursiveDiff($aArray1, $aArray2) {
  $aReturn = array();

  foreach ($aArray1 as $mKey => $mValue) {
    if (array_key_exists($mKey, $aArray2)) {
      if (is_array($mValue)) {
        $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
        if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
      } else {
        if ($mValue != $aArray2[$mKey]) {
          $aReturn[$mKey] = $mValue;
        }
      }
    } else {
      $aReturn[$mKey] = $mValue;
    }
  }
  return $aReturn;
} 

1 Comment

Please add some explanation of how your code would solve the problem.
-1

Lots of ways to go about this. The simplest approach I can think of comes from https://stackoverflow.com/a/29296247/296555.

  • It generates 2 arrays (range)
  • shuffles them (shuffle)
  • takes a subset of each one (array_slice)
  • merges them (array_merge),
  • and finally pulls out the unique values (array_unique).

See http://sandbox.onlinephpfunctions.com/code/46ae8c76f0aa7fa0c84c449b357911ea1d1919db

<?PHP
// http://sandbox.onlinephpfunctions.com/code/46ae8c76f0aa7fa0c84c449b357911ea1d1919db    

// Final array will have a maximum length of $length*2, but it may be shorter. 
$length = 10;

// First
$first = range(20, 60);
shuffle($first);
$first = array_slice($first, 0, $length);

// Second
$second = range(40, 80);
shuffle($second);
$second = array_slice($second, 0, $length);

$merged = array_unique(array_merge($first, $second));

print_r($merged);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.