I think you can achieve via http://www.php.net/manual/en/function.array-walk.php.
I try and write some test for it soon and update post.
P.S: array_map is better(maybe walk not even good for this?)
<?php
function combine($arr1, $arr2) {
return array_map("callback", $arr1, $arr2);
}
function callback($item, &$item2) {
if (is_numeric($item)) {
return max($item, $item2);
}
if (strlen($item2) > strlen($item)) {
return $item2;
}
return $item;
}
class Tests extends PHPUnit_Framework_TestCase {
public function testMergingArrays() {
$arr1 = array(
'boofoo',
'2'
);
$arr2 = array(
'fbfb',
'6'
);
$arr3 = array(
'boofoo',
'6'
);
$this->assertEquals($arr3, combine($arr1, $arr2));
}
}
I got test in place and I could refactor it now. But this implementation does work.