Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite function to array_combine /two arrays from an associative array

Tags:

arrays

php

How can I make two numeric arrays (one for keys one for values - the opposite of array combine)

Source info:

John => Physics,

Mary => Medicine,

Gary => Drama,

Output to

0=>Physics, 

1=>Medicine, 

2=>Drama

and

0=>John,

1=>Mary,

2=>Drama

It seems easy, but I've had no luck.

like image 805
EnglishAdam Avatar asked Nov 27 '11 23:11

EnglishAdam


1 Answers

Call array_keys() and array_values() on your associative array respectively.


If for whatever reason you must have numeric indices that start at 1 (as in your original question before any edits), you'll need to do a little more:

$keys = array_keys($array);
array_unshift($keys, NULL);
unset($keys[0]);

$values = array_values($array);
array_unshift($values, NULL);
unset($values[0]);
like image 143
BoltClock Avatar answered Oct 13 '22 23:10

BoltClock