29

Had a hard time wording this in google so I figure ill ask here.

I have an array like such:

Array ( [a] => 'a' [b] => 'b' [c] => 'c' )

Is there an easy way to convert the keys to numerical values like such? Is there a built in function, or will I have to make one?

Array ( [0] => 'a' [1] => 'b' [2] => 'c' )
0

4 Answers 4

43

You want the array_values function.

Sign up to request clarification or add additional context in comments.

1 Comment

This accepted answer is too short, and I don't know whether the external link will still exist in the future.
19

Use array_values(), as in the manual example:

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

The above example will output:

Array
(
    [0] => XL
    [1] => gold
)

3 Comments

This will work with one-directional arrays, but not multi-dimensional. For that, you'll need a recursive function.
The question was about a one dimensional array.
It sure was. However those looking for a similar solution for a multidimensional array will appreciate knowing up front that this answer won't help them. Don't ever be afraid to explain where ( and where not ) a solution may apply.
5

Try array_values

$arrayWithNumericKeys = array_values($arrayWithRegularKeys);

Comments

4
$my_array = array( [a] => 'a' [b] => 'b' [c] => 'c' )
$my_new_array = array_values($my_array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.