Home
DevCentral
Search
Configure Global Search
Log In
Transactions
T1576
Change Details
Change Details
Old
New
Diff
Two general interest functions could be moved from rTOOLS to rKOT: **Current code** ```lang=php,name=Methods from infographics/parlement-fr.php /** * Gets a count of items, grouped a key. * * This achieves the same result than SQL queries like: * SELECT foo, COUNT(foo) AS c FROM quux GROUP BY foo ORDER BY c DESC; * * @param $array an array, each line being an array * @param $by the grouping key * @return $array an array, each line having the key value as key, and the count as value */ function count_by ($array, $by) { $count = []; foreach ($array as $row) { $key = $row[$by]; if (isset($count[$key])) { $count[$key]++; } else { $count[$key] = 1; } } arsort($count, SORT_NUMERIC); return $count; } /** * Gets a group of items' property, grouped by a key * * @param $array an array, each line being an array * @param $by the grouping key * @return $array an array, each element an array of properties matching the key. */ function group_by($array, $by, $property) { $groups = []; foreach ($array as $row) { $key = $row[$by]; $groups[$key][] = $row[$property]; } ksort($groups, SORT_NUMERIC); return $groups; } ``` **Current use** ```lang=php,name=Example of use from infographics/parlement-fr.php $count = count_by($senateurs, 'born'); $data = []; foreach ($count as $key => $value) { $data[] = "[$key, $value]"; } ``` ``` var senateurs = <?= json_encode(group_by($senateurs, 'born', 'name')); ?>; ``` **Example of use in ...\Collection\OmniArray** ```name=Example of use we could offer <?php $countOfSenateursByYear = new OmniArray($senateurs) ->countBy('born') ->keyValueMap() ->toArray(); ``` ```lang=php <?= new OmniArray($senateurs)->groupBy('born', 'name')->toJSON() ?> ```
Two general interest functions could be moved from rTOOLS to rKOT: **Current code** ```lang=php,name=Methods from infographics/parlement-fr.php /** * Gets a count of items, grouped a key. * * This achieves the same result than SQL queries like: * SELECT foo, COUNT(foo) AS c FROM quux GROUP BY foo ORDER BY c DESC; * * @param $array an array, each line being an array * @param $by the grouping key * @return $array an array, each line having the key value as key, and the count as value */ function count_by ($array, $by) { $count = []; foreach ($array as $row) { $key = $row[$by]; if (isset($count[$key])) { $count[$key]++; } else { $count[$key] = 1; } } arsort($count, SORT_NUMERIC); return $count; } /** * Gets a group of items' property, grouped by a key * * @param $array an array, each line being an array * @param $by the grouping key * @return $array an array, each element an array of properties matching the key. */ function group_by($array, $by, $property) { $groups = []; foreach ($array as $row) { $key = $row[$by]; $groups[$key][] = $row[$property]; } ksort($groups, SORT_NUMERIC); return $groups; } ``` **Current use** ```lang=php,name=Example of use from infographics/parlement-fr.php $count = count_by($senateurs, 'born'); $data = []; foreach ($count as $key => $value) { $data[] = "[$key, $value]"; } ``` ```lang=php var senateurs = <?= json_encode(group_by($senateurs, 'born', 'name')); ?>; ``` **Example of use in ...\Collection\OmniArray** ```name=Example of use we could offer <?php $countOfSenateursByYear = HashMap::from($senateurs) ->countBy('born') ->map(fn($key, $value) => "[$key, $value]") ->toArray(); ``` ```lang=php <?= HashMap::from($senateurs)->groupBy('born', 'name')->toJSON() ?> ``` The last example would benefit from: ```lang=php public function toJSON () : string { return json_encode($this->map); } ```
Two general interest functions could be moved from rTOOLS to rKOT: **Current code** ```lang=php,name=Methods from infographics/parlement-fr.php /** * Gets a count of items, grouped a key. * * This achieves the same result than SQL queries like: * SELECT foo, COUNT(foo) AS c FROM quux GROUP BY foo ORDER BY c DESC; * * @param $array an array, each line being an array * @param $by the grouping key * @return $array an array, each line having the key value as key, and the count as value */ function count_by ($array, $by) { $count = []; foreach ($array as $row) { $key = $row[$by]; if (isset($count[$key])) { $count[$key]++; } else { $count[$key] = 1; } } arsort($count, SORT_NUMERIC); return $count; } /** * Gets a group of items' property, grouped by a key * * @param $array an array, each line being an array * @param $by the grouping key * @return $array an array, each element an array of properties matching the key. */ function group_by($array, $by, $property) { $groups = []; foreach ($array as $row) { $key = $row[$by]; $groups[$key][] = $row[$property]; } ksort($groups, SORT_NUMERIC); return $groups; } ``` **Current use** ```lang=php,name=Example of use from infographics/parlement-fr.php $count = count_by($senateurs, 'born'); $data = []; foreach ($count as $key => $value) { $data[] = "[$key, $value]"; } ``` ```
lang=php
var senateurs = <?= json_encode(group_by($senateurs, 'born', 'name')); ?>; ``` **Example of use in ...\Collection\OmniArray** ```name=Example of use we could offer <?php $countOfSenateursByYear =
new OmniArray
HashMap::from
($senateurs) ->countBy('born')
->keyValueMap(
->map(fn($key, $value) => "[$key, $value]"
) ->toArray(); ``` ```lang=php <?
= new OmniArray
= HashMap::from
($senateurs)->groupBy('born', 'name')->toJSON() ?>
``` The last example would benefit from: ```lang=php public function toJSON () : string { return json_encode($this->map); }
```
Continue