2

I have an array $people. When I do print_r($people), I get the following results:

[people] => Array
(
  [500] => Array
          (
              [firstName] => Fred
              [age] => 19
          )

  [501] => Array
          (
               [firstName] => Bob
               [age] => 12
          )
  [502] => Array
          (
               [firstName] => Steve
               [age] => 52
          )
)

I want to change all the keys to look more "normal", starting at 0, then 1, 2 etc. How can I achieve this? To clarify, I want the resulting array to look like this:

    [people] => Array
(
  [0] => Array
          (
              [firstName] => Fred
              [age] => 19
          )

  [1] => Array
          (
               [firstName] => Bob
               [age] => 12
          )
  [2] => Array
          (
               [firstName] => Steve
               [age] => 52
          )
)
1
  • 2
    Don't iterate, just use array_values() which will reset the keys for you: $people['people'] = array_values($people['people']); Commented Sep 16, 2014 at 9:29

3 Answers 3

3

The built-in function array_values() will take only the values from an array, ignoring the keys and instead returning the array renumbered from zero.

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

Comments

0

Simply like this:

foreach ($people as $value) {
  $people_new[] = $value;
}

Comments

0

Try this

$people['people'] = array_values($people['people']);
print_r($people);

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.