0

I have array of objects, what I want to do is reset the index like 123, 150, 50 to be 0,1,2 . I have done array_values(); but it removes the first array "123".

How can I make the index numbers starting from 0 . to make 123, 150, 50, to become 0,1,2

     array(
    123 =>
      User::__set_state(array(
    _type' => 'student',
    'id' =>'23'}),
    150=>
    User::__set_state(array(
    '_type' => 'student',
   'id' =>'29'}),
    50=>
    User::__set_state(array(
    '_type' => 'student',
    'id' =>'12'})

The output

         array(
     150=>
    User::__set_state(array(
    '_type' => 'student',
   'id' =>'29'}),
    50=>
    User::__set_state(array(
    '_type' => 'student',
    'id' =>'12'})
2
  • array_values removing an index ? strange... can you give us the exact input and output ? Commented Nov 5, 2012 at 10:13
  • thanks, but can you use var_dump instead to write input and output ? Commented Nov 5, 2012 at 10:16

1 Answer 1

1

array_values is correct solution. The little thing is that it does not affect given array, but returns an array. So you have to assign it to another variable. For example:

$arr1 = array(123 => 'test', 1234 => 'test2');
$arr2 = array_values($arr1);
print_r($arr2); //prints: Array ([0] => test [1] => test2)
Sign up to request clarification or add additional context in comments.

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.