5

If I have an array as $keys => $values, how can I get two arrays of $keys and $values?

1
  • I don't understand your question, maybe an example how it should look like? Commented Jul 9, 2009 at 11:10

3 Answers 3

28

Using array_keys() and array_values().

$keys = array_keys($array);
$values = array_values($array);
Sign up to request clarification or add additional context in comments.

Comments

16

array_keys and array_values will return a numerical array of the keys/values of a given array:

$keys = array_keys($array);
$values = array_values($array);

Or if you want a foreach solution:

$keys = array();
$values = array();
foreach ($array as $key => $value) {
    $keys[] = $key;
    $values[] = $value;
}

Comments

12

You can use array_keys and array_values.

$keys   = array_keys($thearray);
$values = array_values($thearray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.