Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I need PHP code to rearrange the array index after the array_unique function has been called...
i used
array_unique($a1)

i got a result as
Array ( [0] => [1] => Apparel & Accessories [130] => Baby & Toddler Toys [158] => Baby Stationery [162] => Bathing & Skin Care [187] => Car Seats & Accessories [197] => Diapering [227] => Feeding [275] => Gear [296] => Gifts [319] => Health & Baby Care [328] => Nursery [415] => Potty Training [421] => Pregnancy & Maternity [431] => Safety [445] => Shoes [532] => Strollers )

but i need
Array ( [0] => [1] => Apparel & Accessories [2] => Baby & Toddler Toys [3] => Baby Stationery [4] => Bathing & Skin Care [5] => Car Seats & Accessories [6] => Diapering [7] => Feeding [8] => Gear [9] => Gifts [10] => Health & Baby Care [11] => Nursery [12] => Potty Training [13] => Pregnancy & Maternity [14] => Safety [15] => Shoes [16] => Strollers )
Posted

1 solution

if you want to close the gaps into the keys after using array_unique() you can use array_values() afterwards. Example:


PHP
a = array("one", "two", "two", "three")
a = array_unique(a);
/* will lead to:
a[0] = "one"
a[1] = "two"
a[3] = "three"
*/
a = array_values(a);
/* Now we've got:
a[0] = "one"
a[1] = "two"
a[2] = "three"
*/
?>


Hope this helps..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900