0

I have an array:

 $array = array(3=>'hi', 4=>'hello');

How can i start it from 0 i.e. $array = array(0=>'hi', 1=>'hello');

I tried using sort but it sorted the values also which i dont want. I can do it using foreach but i dont want to use loops. Is there any function or something?

4 Answers4

2

try array_values()

in document:

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

out:

Array
(
    [0] => XL
    [1] => gold
) 
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
jiang
  • 170
  • 4
  • Thanks..that worked!! Will accept in 10 minutes – user3048231 Nov 29 '13 at 06:25
  • Note: Keep in mind that with an associative array calling `array_values()` will remove the reference to the index (because it creates a new array with the `values`, indeed). With a numeric array like in Op's case the solution is right, I would have used this kind of example if I were you, makes things look more clear :) – Damien Pirsy Nov 29 '13 at 06:25
1

it can be done using array_values, but if you want to preserve the original keys , just check this link

(Convert associative array into indexed)

Community
  • 1
  • 1
awsumnik
  • 116
  • 4
0

Try this:

$array = array(3=>'hi', 4=>'hello');
$array = array_values($array);
var_dump($array);

It will returns you this:

array(2) { 
[0]=> string(2) "hi" 
[1]=> string(5) "hello" 
}
Dinistro
  • 5,701
  • 1
  • 30
  • 38
0

Please try executing following code snippet

  <?php
   $keys=range(0,1);
   $values=array('hi','hello');
   $final=array_combine($keys,$values);
   print_r($final);
?>
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26