note 127129 added to function.array-unique

From: Date: Tue, 24 May 2022 09:45:06 +0000
Subject: note 127129 added to function.array-unique
Groups: php.notes 
Request: Send a blank email to php-notes+get-218521@lists.php.net to get a copy of this message
Because of PHP comparaisons modalities, you can never distinguish null from others falsy values. Note the absorbing nature of true and false booleans in mix types array. <?php $a = [true, false, null, '', '0', '123', 0, 123]; foreach (['SORT_REGULAR', 'SORT_NUMERIC', 'SORT_STRING', 'SORT_LOCALE_STRING'] as $flag) { $a_new = array_unique($a, constant($flag)); echo "{$flag} ==> "; var_dump($a_new); } /* Gives : SORT_REGULAR ==> array(2) { [0]=> bool(true) [1]=> bool(false) } SORT_NUMERIC ==> array(3) { [0]=> bool(true) [1]=> bool(false) [5]=> string(3) "123" } SORT_STRING ==> array(4) { [0]=> bool(true) [1]=> bool(false) [4]=> string(1) "0" [5]=> string(3) "123" } SORT_LOCALE_STRING ==> array(4) { [0]=> bool(true) [1]=> bool(false) [4]=> string(1) "0" [5]=> string(3) "123" } */ ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.12.131 (proxied: 2a01:e0a:968:1840:a539:8138:275a:4564) ---- Manual Page -- https://php.net/manual/en/function.array-unique.php Edit -- https://main.php.net/note/edit/127129 Del: integrated -- https://main.php.net/note/delete/127129/integrated Del: useless -- https://main.php.net/note/delete/127129/useless Del: bad code -- https://main.php.net/note/delete/127129/bad+code Del: spam -- https://main.php.net/note/delete/127129/spam Del: non-english -- https://main.php.net/note/delete/127129/non-english Del: in docs -- https://main.php.net/note/delete/127129/in+docs Del: other reasons-- https://main.php.net/note/delete/127129 Reject -- https://main.php.net/note/reject/127129 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#218521) next »