I have two arrays, $A and $B. Both arrays have the same length, but array $A will always either have the same number of non-empty elements as $B or it will have less non-empty elements than $B.
So if,
$B = array("john","adams","sandwich");
then $A could either be,
$A = array("bacon","ham","juice");
or it could be,
$A = array("bacon","","");
I’m trying to match the empty elements of $B to $A. So I can get,
$B = array("john","","");
I’m currently doing it through the following code:
$q = count($A) - 1;
$l = count($A) - count(array_filter($A));
$i = 1;
while($i <= $l){
$B[$q] = "";
$i++;
$q--;
}
But this method is extremely slow and might cause problems considering that I’m going to be doing it several times. Do you guys have any suggestions on how to do this more efficiently? The arrays will never be larger than 20 elements, but this is still time consuming for some reason.
Following code will work in all cases, whether empty values are on any position in array: