note 73584 deleted from function.array-merge by cmb
| From: | cmb@php.net | Date: | Sun, 29 Sep 2019 07:33:28 +0000 |
| Subject: | note 73584 deleted from function.array-merge by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212401@lists.php.net to get a copy of this message | ||
Note Submitter: Marce!
----
I have been searching for an in-place merge function, but couldn't find one. This function
merges two arrays, but leaves the order untouched.
Here it is for all others that want it:
function inplacemerge($a, $b) {
$result = array();
$i = $j = 0;
if (count($a)==0) { return $b; }
if (count($b)==0) { return $a; }
while($i < count($a) && $j < count($b)){
if ($a[$i] <= $b[$j]) {
$result[] = $a[$i];
if ($a[$i]==$b[$j]) { $j++; }
$i++;
} else {
$result[] = $b[$j];
$j++;
}
}
while ($i<count($a)){
$result[] = $a[$i];
$i++;
}
while ($j<count($b)){
$result[] = $b[$j];
$j++;
}
return $result;
}