revo
revo

Reputation: 48711

Change the keys of an array

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

Upvotes: 1

Views: 112

Answers (2)

maressyl
maressyl

Reputation: 983

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

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

Upvotes: 0

kapa
kapa

Reputation: 78681

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

Upvotes: 5

Related Questions