merge php arrays like array_combine, but not glue, but get a nested array?

array 1

Array ( [0] => xxx1 [1] => xxx2 [2] => xxx3 [3] => xxx4 [4] => xxx5 [5] => xxx6 ) 

array 2

 Array ( [0] => yyy1 [1] => yyy2 [2] => yyy3 [3] => yyy4 [4] => yyy4 [5] => yyy4 ) 

get out

 Array ( [yyy1] => xxx1 [yyy2] => xxx2 [yyy3] => xxx3 [yyy4] => Array (xxx4, xxx5, xxx6) ) 

UPDATE in general the problem is as follows: 2 arrays, both equal in the number of pairs of key => value (this condition is constant)

further: in array 1 there is a certain value that we catch, say it is "X", it can repeat n times.

you need to get the keys "X" and for these keys create an array of the values ​​of mossive2

array1

 ( [0] => tresh1 [1] => tresh2 [2] => X [3] => X [4] => tresh3 ) 

array2

 ( [0] => tresh11 [1] => tresh22 [2] => need1 [3] => need2 [4] => tresh33 ) 

at the exit

 ( [X] => Array(need1, need2) ) 
  • What is the basis of this logic? It is not clear to me personally ... what have you tried? What did not work out? - Alexey Shimansky
  • @ Alexey Shimansky, I tried array_combine to get: ([yyy1] => xxx1 [yyy2] => xxx2 [yyy3] => xxx3 [yyy4] => xxx6) in "yyy4" only the last value, but I need all the values. - Alexey Vasilyev
  • the logic is still not clear .. what will happen if the array acting as keys will be yyy1/yyy2/yyy3/yyy5/yyy6/yyy4 or in an array with values xxx1/xxx1/xxx2/xxx3/xxx4/xxx4 .... . etc. - Alexey Shimansky
  • @ Alexey Shimansky added UPDATE with a clearer description. - Alexey Vasilyev

3 answers 3

 $first = [ 'yyy1', 'yyy2', 'yyy3', 'yyy4', 'yyy4', 'yyy4' ]; $second = [ 'xxx1', 'xxx2', 'xxx3', 'xxx4', 'xxx5', 'xxx6' ]; $countKeys = count($first); $new = []; for ($i = 0; $i < $countKeys; $i++) { if(array_key_exists($first[$i], $new)){ if(is_array($new[$first[$i]])){ $new[$first[$i]][]=$second[$i]; }else{ $new[$first[$i]] = [$new[$first[$i]], $second[$i]]; } }else{ $new[$first[$i]] = $second[$i]; } } 
  • What you need, thank you very much !!!! - Alexey Vasilyev
 $keys = array( 'yyy1', 'yyy2', 'yyy3', 'yyy4', 'yyy4', 'yyy4', ); $values = array( 'xxx1', 'xxx2', 'xxx3', 'xxx4', 'xxx5', 'xxx6', ); $countElements = count($keys); $output = array(); for ($index = 0; $index < $countElements; $index++) { $key = $keys[$index]; $value = $values[$index]; $keyExists = array_key_exists($key, $output); if (!$keyExists) { $output[$key] = $value; } else { $output[$key] = array_merge((array) $output[$key], array($value)); } } 

I apologize wrote on the phone corrected errors.

result:

 Array ( [yyy1] => xxx1 [yyy2] => xxx2 [yyy3] => xxx3 [yyy4] => Array ( [0] => xxx4 [1] => xxx5 [2] => xxx6 ) ) 
  • your function has the same result as array_combine, it does not suit me, it writes the key with the last value ... Array ([yyy1] => xxx1 [yyy2] => xxx2 [yyy3] => xxx3 [yyy4] => xxx6) - Alexey Vasilyev
 $a = ['y1', 'y2', 'y3', 'y3', 'y4', 'y4', 'y4']; $b = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7']; 

 // 1 $result = []; foreach ($a as $key => $val) { $result = array_merge_recursive($result, [$val => $b[$key]]); } 

 // 2 $result = array_reduce(array_keys($a), function (&$carry, $key) use ($a, $b) { return $carry = array_merge_recursive($carry, [$a[$key] => $b[$key]]); }, []); 

 // 3 $result = []; array_walk($a, function ($val, $key, $b) use (&$result) { $result = array_merge_recursive($result, [$val => $b[$key]]); }, $b); 

 // 4 $result = []; array_map(function ($aVal, $bVal) use (&$result) { $result = array_merge_recursive($result, [$aVal => $bVal]); }, $a, $b); 

 // 5 $result = []; foreach ($a as $key => $val) { $result[$val] = array_key_exists($val, $result) ? array_merge((array) $result[$val], (array) $b[$key]) : $b[$key]; };