2

How do I extract the keys and the values of an array into two separate arrays, with autoincrementing integers as keys?

Ex:

Base:
Array ( [MX] => 13 ); 

Array 1:
Array ( [0] => MX ); 

Array 2:
Array ( [0] => 13 ); 

4 Answers 4

4

use array_keys() and

array_values()

Array Keys

$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

Result

Array

(
    [0] => 0
    [1] => color
)

Array Values

$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));

Result

Array
(
    [0] => XL
    [1] => gold
)
Sign up to request clarification or add additional context in comments.

Comments

3

Use array_keys() and array_values().

$array1 = array_keys($base);

$array2 = array_values($base);

Comments

1
$array1= array_keys($base_array);
$array2= array_values($base_array);

Comments

0

You can use array_keys() and array_values

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.