|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-03-28 15:36 UTC] dpfender44 at gmail dot com
Description: ------------ The function array_unique produces bad results with an array of simple string values. Many unset elements result in the new array. The test program shows good results with my own do_array_unique function and bad results with PHP array_unique. Test script: --------------- http://shuffle.djpnet.dyndns.org/shuffle3_php.txt PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 10:00:02 2025 UTC |
> In this case there are no keys in the array there is nothing like an array without keys in PHP > each has a numeric index provided by PHP when the element is assigned so the array has keys as *every* array has by defintion because hash tables are impossible without php > print_r(['a', 'b', 'c']); Array ( [0] => a [1] => b [2] => c ) > If the count of elements in the array shows 30 elements, > then each element index 0 thru 29 should have a value. and here we are back at "Note that keys are preserved" > Otherwise, the new array makes no sense again: "Note that keys are preserved"normal, expected behavior php > print_r(['x', 'a', 'x', 'b', 'a', 'b']); Array ( [0] => x [1] => a [2] => x [3] => b [4] => a [5] => b ) php > print_r(array_unique(['x', 'a', 'x', 'b', 'a', 'b'])); Array ( [0] => x [1] => a [3] => b )and to finish that discussion: "then each element index 0 thru 29 should have a value. Otherwise, the new array makes no sense" is nonsense because in most caes nobody iterates arrays with a for-each loop or care about the numeric keys at all if you care in a specific usecaes just call array_values(9 and you are done, but don#t insist in the performance penalty doing that behind the scenes all the time even if it don't matter for the code the language has everything you need, just use it php > print_r(array_values(array_unique(['x', 'a', 'x', 'b', 'a', 'b']))); Array ( [0] => x [1] => a [2] => b )