note 128757 added to function.array-filter

From: Date: Tue, 25 Jul 2023 06:20:39 +0000
Subject: note 128757 added to function.array-filter
Groups: php.notes 
Request: Send a blank email to php-notes+get-220718@lists.php.net to get a copy of this message
The fact that array_filter preserves keys makes partitioning an array into [elements that pass the test, elements that fail the test] quite easy. In essence: <?php function partition($array, $test) { $pass = array_filter($array, $test); $fail = array_diff_key($array, $pass); return [false => $fail, true => $pass]; } ?> The array_diff_key call is key; indexing the returned array as shown allows lines like "$failures = $partition[false];" to do the right thing (the booleans get converted to integers of course, but it's consistent and self-documenting). ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.168 (proxied: 125.238.198.150) ---- Manual Page -- https://php.net/manual/en/function.array-filter.php Edit -- https://main.php.net/note/edit/128757 Del: integrated -- https://main.php.net/note/delete/128757/integrated Del: useless -- https://main.php.net/note/delete/128757/useless Del: bad code -- https://main.php.net/note/delete/128757/bad+code Del: spam -- https://main.php.net/note/delete/128757/spam Del: non-english -- https://main.php.net/note/delete/128757/non-english Del: in docs -- https://main.php.net/note/delete/128757/in+docs Del: other reasons-- https://main.php.net/note/delete/128757 Reject -- https://main.php.net/note/reject/128757 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220718) next »