Question

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

Was it helpful?

Solution

Using array_keys() and array_values().

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

OTHER TIPS

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;
}

You can use array_keys and array_values.

$keys   = array_keys($thearray);
$values = array_values($thearray);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top