im trying to fill values from array1 as keys for array2 but i got problem if keys are duplicate:
$arr1 = array( array(0 => "1", 1 => "1"), array(0 => "2", 1 => "2"), ); $arr2 = array( array(0 => "a", 1 => "b"), array(0 => "c", 1 => "d"), ); $result = []; for ($i = 0; $i < count($arr1); $i++) { $result[$i] = array_combine($arr1[$i], $arr2[$i]); }
result i got:
$result = array( array(1 => "b"), array(2 => "d"), );
I need it like this:
$result = array( array(1 => "a", 1 => "b"), array(2 => "c", 2 => "d"), );
Modified output I want now: (after looking all comments/answer)
array ( 0 => array ( 0 => array( 0 => '1', 1 => 'a', ), 1 => array( 0 => '1', 1 => 'b', ), ), 1 => array ( 0 => array( 0 => '1', 1 => 'c', ), 1 => array( 0 => '1', 1 => 'd', ), ), );
Thank you!
Advertisement
Answer
1st solution : You cannot get what you want as same indexes in php array got over-write:https://3v4l.org/r934K
What best possible you can get is:
$result = []; foreach($arr1 as $key=>$value){ $result[array_unique($value)[0]] = $arr2[$key]; }
Output: https://3v4l.org/DfPU3
2nd soultion : For the output what you want, you need to apply one more foreach()
$result = []; foreach($arr1 as $key=>$value){ foreach($value as $k=>$val){ $result[$key][$k] = [$val,$arr2[$key][$k]]; } } print_r($result);
Output : https://3v4l.org/u8JmX