-3

Is it possible to convert this array:

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

To this array:

array(
  array(
    'A',
    'B',
  ),
  array(
    'C',
    'D',
  ),
)
mickmackusa
  • 37,596
  • 11
  • 75
  • 105
  • 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
  • 184,866
  • 48
  • 414
  • 792
0
$source = array(
  'A' => 'B',
  'C' => 'D',
)

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

var_dump($result);
xdazz
  • 154,648
  • 35
  • 237
  • 264
GolezTrol
  • 111,943
  • 16
  • 178
  • 202