array_unique and then renumbering keys [duplicate]
Asked Answered
M

1

76

Possible Duplicate:
Re-index numeric array keys

I have an array as follows

Array
(
    [0] => 15/11/2012 - 18/11/2012
    [1] => 15/11/2012 - 18/11/2012
    [2] => 15/11/2012 - 18/11/2012
    [3] => 15/11/2012 - 18/11/2012
    [4] => 19/12/2012 - 24/12/2012
    [5] => 24/12/2012 - 01/01/2013
    [6] => 24/12/2012 - 01/01/2013
    [7] => 16/01/2013 - 01/02/2013
)

I am using array_unique to remove the duplicates which is giving me

    Array
(
    [0] => 15/11/2012 - 18/11/2012
    [4] => 19/12/2012 - 24/12/2012
    [5] => 24/12/2012 - 01/01/2013
    [7] => 16/01/2013 - 01/02/2013
)

How can I change the keys so that they are consecutive - as below

    Array
(
    [0] => 15/11/2012 - 18/11/2012
    [1] => 19/12/2012 - 24/12/2012
    [2] => 24/12/2012 - 01/01/2013
    [3] => 16/01/2013 - 01/02/2013
)

thanks in advance

Maffick answered 28/11, 2012 at 1:8 Comment(1)
$array = array_values($your_output_array) array_values() will renumber the keys.Bernard
S
156

Easiest way would be to put them into a new array either via a loop, or better yet array_values function.

$new_array = array_values($original_array)

More information

Servomechanical answered 28/11, 2012 at 1:11 Comment(1)
For me this solution didn't work. I forced to use this: array_values(array_unique($array)) and it worked correctlyImmoderate

© 2022 - 2024 — McMap. All rights reserved.