0

For example;

$array[16] = 20;
$array[18] = 8;
$array[27] = 95;

How can I change array[] key value start 0 like this;

$array[0] = 20;
$array[1] = 8;
$array[2] = 95;

I don't know key value. They can be change. $array[16] can be $array[21]

2
  • You're not trying to change the order of the array, right? Just reset the first key to zero and increment from there? Commented Nov 18, 2012 at 20:23
  • Looping over them will do the trick. But what is the need for resetting the keys Commented Nov 18, 2012 at 20:24

3 Answers 3

5

Just assign the values to a new array with the same name:

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

Comments

2

You can just use array_values

$array = array_values($array);

1 Comment

I agree, this is the most efficient for PHP imho.
1

just use array_values like this array_values($your_array) and you will have an array starting from index zero.

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.