php - is it possible to replace different array value(s) with different keys from different array -
[1] => ooooooo [2] => ooooooo [3] => ooooooo [4] => ooooooo [5] => ------------empty [6] => ooooooo [7] => ooooooo [8] => ooooooo [9] => ooooooo [10] => ------------empty [11] => ooooooo [12] => ooooooo [13] => ------------empty [14] => ooooooo [15] => ooooooo
replace array1 above: "------------empty" below array2, keeping keys of array1
[1] => xxxxx [2] => yyyyy [3] => zzzzz
so asks me add explanation---a picture paint more thousand words(i believe?) heck should more. had wonderful time last christmas, hope did :)
result this:
maybe should it's necessity find , replace automaticlly--so no manual or individual inputs. each "---empty" possibly @ different position on every call
[1] => ooooooo [2] => ooooooo [3] => ooooooo [4] => ooooooo [5] => xxxxx [6] => ooooooo [7] => ooooooo [8] => ooooooo [9] => ooooooo [10] => yyyyy [11] => ooooooo [12] => ooooooo [13] => zzzzz [14] => ooooooo [15] => ooooooo
well yes, can say:
array1[index] = array2[another_index];
but have careful 2 arrays contain same type indexes within size of arrays
i guess wanted is:
array1[5] = array2[1]; // replace '------------empty` 'xxxxx' array1[10] = array2[2]; // replace '------------empty` 'yyyyy' array1[13] = array[3]; // replace '------------empty` 'zzzzz'
note in programming languages, array indexes start @ 0 indexes should less 1.
here pseudo-code in loop:
int = 0; // index array1 int j = 0; // index array2 while( < array1.size ) { if(array1[i] equals "------------empty" , j < array2.size) { array1[i] = array2[j]; increment j; } increment i; }
edit:
based on comment, here php code in loop.
$size1 = count($array1); $size2 = count($array2); $i = 0; $j = 0; while($i < $size1) { if($array1[$i] == "------------empty" && $j < $size2) { $array1[$i] = $array2[$j]; $j = $j + 1; } $i = $i + 1; }
if comparing objects should take @ this
Comments
Post a Comment