0

The following code:

$ids=['one','two','three','two','four'];
echo('<pre>'.print_r($ids,1).'</pre>');
$ids = array_unique($ids);
echo('<pre>'.print_r($ids,1).'</pre>');

produces the following:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => two
    [4] => four
)

Array
(
    [0] => one
    [1] => two
    [2] => three
    [4] => four
)

How do I preserve consecutive array indexes and produce the following:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)
user1032531
  • 22,993
  • 61
  • 191
  • 346
  • 1
    @PrototypeChain Thank you. Sorry, I searched and didn't find it. – user1032531 Jun 11 '16 at 19:22
  • it's a strange question as for `6,852` reputation – RomanPerekhrest Jun 11 '16 at 19:33
  • PrototypeChain first commented that http://stackoverflow.com/questions/13596128/array-unique-and-then-renumbering-keys was a possible duplicate, but then deleted the comment. I don't agree that http://stackoverflow.com/questions/591094/how-do-you-reindex-an-array-in-php directly answered my question, but PrototypeChain's did. – user1032531 Jun 11 '16 at 20:10

2 Answers2

1

Add one more line array_values($ids)

D.Dimitrioglo
  • 3,023
  • 2
  • 20
  • 35
0

You can use array_values.

Check out the manual: https://secure.php.net/manual/en/function.array-values.php

Gargamel
  • 58
  • 4