1

I've an array like below :

$array = array('string'=>'hello','somethingeElse'=>'how r u', ....);

I wanna change the keys of array to numeric values (consecutive):

$array = array('1'=>'hello','2'=>'how r u','3'=>....);

any help would be appreciated ;)

2
  • Just loop through the array and change the keys as needed. Commented Nov 7, 2011 at 15:55
  • 2
    This is a pretty straighforward task. What have you tried? Commented Nov 7, 2011 at 15:57

2 Answers 2

5

You could use the array_values() function, which will basically do what you say.

array_values() returns all the values from the input array and indexes numerically the array.

Example:

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

Comments

0

If you want to avoid PHP loops, you may try something like :

$newArray = array_combine(range(1, count($array)), array_values($array));

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.