poe Posted March 31, 2008 Share Posted March 31, 2008 if i have an array like so: colorAry = array('grn1', 'grn2', 'grn3', 'blue1', 'blue2', 'red1', 'red2', 'red3', 'org1', 'org2') and i have a second array like so: altcolorAry = array('grn1a', 'blue1a', 'red1a') then i shuffle(colorAry) so my colorAry now looks like:: newcolorAry = array('red2', 'org1', 'grn2', 'blue1', 'grn3', 'red1', 'grn1', 'red3', 'blue2', 'org2') how do i add the contents of altcolorAry so that it will fit in alpha/numericly: 'grn1a' so that it fits in after grn1 'blue1a' so that it fits in after blue1 'red1a' so that it fits in after red1 newcolorAry = array('red2', 'org1', 'grn2', 'blue1', 'blue1a', 'grn3', 'red1' 'red1a', 'grn1', 'grn1a', 'red3', 'blue2', 'org2') thanks chris Link to comment https://forums.phpfreaks.com/topic/98889-merge-arrays/ Share on other sites More sharing options...
sasa Posted March 31, 2008 Share Posted March 31, 2008 try <?php function ar_add_fit($arr, $add){ $t = array_merge($arr, $add); sort($t); $i = 0; $out = array(); $tmp = array(); while (in_array($t[$i], $add)){ $out[] = $t[$i]; $i++; } for (;$i < count($t){ $k = $t[$i]; $tmp[$k] = array(); $i++; while (in_array($t[$i], $add)){ $tmp[$k][] = $t[$i]; $i++; } } foreach ($arr as $k){ $out[] = $k; foreach ($tmp[$k] as $v) $out[] = $v; } return $out; } $colorAry = array('grn1', 'grn2', 'grn3', 'blue1', 'blue2', 'red1', 'red2', 'red3', 'org1', 'org2'); $altcolorAry = array('grn1a', 'blue1a', 'red1a'); shuffle($colorAry); $y = ar_add_fit($colorAry, $altcolorAry); print_r($y); ?> Link to comment https://forums.phpfreaks.com/topic/98889-merge-arrays/#findComment-506012 Share on other sites More sharing options...
poe Posted March 31, 2008 Author Share Posted March 31, 2008 awsome... thanks can you explain a bit so i can try learn what you did thanks Link to comment https://forums.phpfreaks.com/topic/98889-merge-arrays/#findComment-506016 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.