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?

0

4 Answers 4

2

try array_values()

in document:

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

out:

Array
(
    [0] => XL
    [1] => gold
) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks..that worked!! Will accept in 10 minutes
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 :)
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)

Comments

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" 
}

Comments

0

Please try executing following code snippet

  <?php
   $keys=range(0,1);
   $values=array('hi','hello');
   $final=array_combine($keys,$values);
   print_r($final);
?>

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.