I have 2 arrays that are not equal but need to merge them into 1 array $a = array('books', 'clothes', 'dvds', 'jewelry', 'toys'); $b = array('0001', '0001', '0001', '0001', '0001', '0002', '0002', '0002', '0002', '0002'); Final need merge the arrays like this: $merge = array('0001,books', '0001,clothes', '0001,dvds', '0001,jewelry', '0001,toys', '0002,books', '0002,clothes', '0002,dvds', '0002,jewelry', '0002,toys'); Thanks
<?php //the function that makes the merge as you want it to be : function customMerge($a,$b) { $ret = array(); if(is_array($a) && count($a) > 0 && is_array($b) && count($b) > 0) { $rounds = count($b) / count($a); for($i = 0 ; $i < $rounds ; $i++) { foreach($a as $key=>$value) { $b_val = $b[$i+(count($a)*$i)]; array_push($ret,$b_val.','.$value); } } } return $ret; } //define your array's $a = array('books','clothes','dvds','jewelry','toys'); $b = array('0001','0001','0001','0001','0001','0002','0002','0002','0002','0002'); //run and print the resulting array echo '<pre>'; print_r(customMerge($a,$b)); echo '</pre>'; ?> PHP: