I have an array (sizeofarray = 5)
$city[0] = 'Newyork'
$city[1] = 'Paris'
$city[2] = 'Paris'
$city[3] = 'Newyork'
$city[4] = 'Amsterdam'
(sizeofarray = 5)
I used array_unique() to remove dupes and got this:
$city[0] = 'Newyork'
$city[1] = 'Paris'
$city[4] = 'Amsterdam'
(sizeofarray = 3)
But now I want this:
$city[0] = 'Newyork'
$city[1] = 'Paris'
$city[2] = 'Amsterdam'
(sizeofarray = 3)
Is there some function to achieve this?
Use
array_filterafter you run the array througharray_unique:From the documentation:
You may run into an error, however, if one of your array elements is equal to
"0", as when converted to boolean, its value isfalse. In that case, you’ll have to use a custom callback function.In your question, it appears that only the array indexing is out of order, and there are not actual empty string elements in your array. Try running the array though
array_valuesafterarray_uniqueto reset the indexing: