Skip to content

Instantly share code, notes, and snippets.

@Stefany93
Created November 13, 2015 17:35
Show Gist options
  • Save Stefany93/691ba3c7494a1011cda1 to your computer and use it in GitHub Desktop.
Save Stefany93/691ba3c7494a1011cda1 to your computer and use it in GitHub Desktop.
<?php
// Task: Design a function that takes two array-parameters containing sorted lists and merges them into one array,
// in a way that results into a sorted array containing all elements of the original two lists.
$first_array = [1, 2];
$second_array = [4, 3, 11];
function my_merge(array $first_array, array $second_array) {
$rv = array();
$biggest_array = 0;
// ....
$count_first_array = count($first_array);
$count_second_array = count($second_array);
if($count_first_array > $count_second_array)
{
$biggest_array = $count_first_array;
}else{
$biggest_array = $count_second_array;
}
for($x = 0; $x <$biggest_array; $x++)
{
if(isset($first_array[$x]))
{
$rv[] = $first_array[$x];
}
if(isset($second_array[$x]))
{
$rv[] = $second_array[$x];
}
}
sort($rv);
return $rv;
}
print_r(my_merge($first_array, $second_array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment