-3

Is it possible to convert this array:

array(
  'A' => 'B',
  'C' => 'D',
)

To this array:

array(
  array(
    'A',
    'B',
  ),
  array(
    'C',
    'D',
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • This is effectively calling for the transposition of an array containing the keys and an array containing the values. https://stackoverflow.com/questions/797251/transposing-multidimensional-arrays-in-php – mickmackusa Apr 23 '22 at 02:44

2 Answers2

5

You are probably looking for the array_map (builds pairings based on existing arrays, see Example #4 Creating an array of arrays on the manual page) and the array_keys (all keys of an array) functions:

array_map(null, array_keys($array), $array));
hakre
  • 193,403
  • 52
  • 435
  • 836
0
$source = array(
  'A' => 'B',
  'C' => 'D',
)

foreach ($source as $key => $value){
  $result[] = array($key, $value);
}

var_dump($result);
xdazz
  • 158,678
  • 38
  • 247
  • 274
GolezTrol
  • 114,394
  • 18
  • 182
  • 210