【发布时间】:2014-05-14 02:41:20
【问题描述】:
我有以下数组
$arr1 = array(
array('ctype'=>'fr', 'type'=>'fr'),
array('ctype'=>'fasdf', 'type'=>'fr'),
array('ctype'=>'fdfdf', 'type'=>'fr'),
array('ctype'=>'fhjdf', 'type'=>'fr'),
array('ctype'=>'fsf', 'type'=>'fr'),
array('ctype'=>'vndf', 'type'=>'fr'),
array('ctype'=>'fnsdf', 'type'=>'fr')
);
$arr2 = array(
array('ctype'=>'fr', 'type'=>'ln'),
array('ctype'=>'fasdf', 'type'=>'ln'),
array('ctype'=>'fayf', 'type'=>'ln'),
array('ctype'=>'fauf', 'type'=>'ln')
);
我想将此数组合并为一个,但合并为相差 2 之类的块
$main_arr = array(
array('ctype'=>'fr', 'type'=>'fr'),
array('ctype'=>'fasdf', 'type'=>'fr'),
array('ctype'=>'fr', 'type'=>'ln'),
array('ctype'=>'fasdf', 'type'=>'ln'),
array('ctype'=>'fdfdf', 'type'=>'fr'),
array('ctype'=>'fhjdf', 'type'=>'fr'),
array('ctype'=>'fayf', 'type'=>'ln'),
array('ctype'=>'fauf', 'type'=>'ln')
array('ctype'=>'fsf', 'type'=>'fr'),
array('ctype'=>'vndf', 'type'=>'fr'),
array('ctype'=>'fnsdf', 'type'=>'fr')
);
如您所见,我想从 arr1 中获取 2 个值,然后从 arr2 中获取 2 个值,如果该值不存在于示例 $arr2 中,则从 $arr1 获取并推送值。我用array_chunks 和array_map 尝试过,但无法达到预期。
这是我尝试过的:
// Divide foursome into chunks
$fr = array_chunk($arr1, 2);
$ln = array_chunk($arr2, 2);
// Can't provide the expected result
$main = array_map(NULL, $fr, $ln);
【问题讨论】: