note 128915 added to function.array-count-values
| From: | Rmr at main dot php dot net | Date: | Tue, 26 Sep 2023 10:50:16 +0000 |
| Subject: | note 128915 added to function.array-count-values | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220874@lists.php.net to get a copy of this message | ||
If you have a multidimensional array with unknown dimensions, you cannot use this function, use
instead:
<?php
function array_count_recursive(array $arr): array {
$occurrences = [] ;
array_walk_recursive( $arr, function($value, $key) use (&$occurrences) {
@ $occurrences[$value]++;
// @ to surpress warnings "Undefined array key". In php8 you can also use
// $occurrences[$value] = ($occurrences[$value] ?? 0) + 1
});
return $occurrences;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 85.145.75.206)
----
Manual Page -- https://php.net/manual/en/function.array-count-values.php
Edit -- https://main.php.net/note/edit/128915
Del: integrated -- https://main.php.net/note/delete/128915/integrated
Del: useless -- https://main.php.net/note/delete/128915/useless
Del: bad code -- https://main.php.net/note/delete/128915/bad+code
Del: spam -- https://main.php.net/note/delete/128915/spam
Del: non-english -- https://main.php.net/note/delete/128915/non-english
Del: in docs -- https://main.php.net/note/delete/128915/in+docs
Del: other reasons-- https://main.php.net/note/delete/128915
Reject -- https://main.php.net/note/reject/128915
Search -- https://main.php.net/manage/user-notes.php