note 126817 added to function.array-reduce
| From: | itsunclexo at gmail dot com | Date: | Tue, 01 Feb 2022 11:15:21 +0000 |
| Subject: | note 126817 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217565@lists.php.net to get a copy of this message | ||
Let's see an example of array_reduce() to get the frequency of letters.
<?php
$items = "Hello";
$frequencies = array_reduce(str_split($items),
function($result, $item) {
if (isset($result[$item])) {
$result[$item] += 1;
} else {
$result[$item] = 1;
}
return $result;
},
[] // note the initial is an array
);
print_r($frequencies);
?>
and output should be like:
Array
(
[H] => 1
[e] => 1
[l] => 2
[o] => 1
)
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 103.121.60.46)
----
Manual Page -- https://php.net/manual/en/function.array-reduce.php
Edit -- https://main.php.net/note/edit/126817
Del: integrated -- https://main.php.net/note/delete/126817/integrated
Del: useless -- https://main.php.net/note/delete/126817/useless
Del: bad code -- https://main.php.net/note/delete/126817/bad+code
Del: spam -- https://main.php.net/note/delete/126817/spam
Del: non-english -- https://main.php.net/note/delete/126817/non-english
Del: in docs -- https://main.php.net/note/delete/126817/in+docs
Del: other reasons-- https://main.php.net/note/delete/126817
Reject -- https://main.php.net/note/reject/126817
Search -- https://main.php.net/manage/user-notes.php