Re: [PHP-DEV] RFC [Discussion] array_column results grouping

2021-12-04 Thread Hendra Gunawan
>
> Hello, this is a proposal to upgrade the functionality of
> `array_column` to return an indexed array for all of the matched data
> instead of a single result.
>
> the RFC had been created here
> https://wiki.php.net/rfc/array_column_results_grouping and the PR is
> in here https://github.com/php/php-src/pull/7698.
>
> However, I am not sure, if we need to add a fourth parameter to the
> function to change its behavior or we can add it as a new function,
> something like `array_column_group` or any other name.
>

Hai Hassan.

It seems that your proposal is just save no more than 3 lines compared with
`foreach`. Moreover, `foreach` can do better than your version.

foreach ($persons as $p) {
$groups[$p["education"]][$p["sex"]][MORE_SUBGRUP][] = $p;
}


output:

$groups = [
"elementary" => [
"female" => [
MORE_SUBGRUP_1 => [
0 => [...],
1 => [...]
],
MORE_SUBGRUP_2 => [
0 => [...],
1 => [...]
],
...
],
"male" => [
MORE_SUBGRUP_1 => [
0 => [...],
1 => [...]
],
MORE_SUBGRUP_2 => [
0 => [...],
1 => [...]
],
...
],
],
"highschool" => [
...
],
...
];

my suggestions are:
- don't limit to array => involve object as well
- offer reducer callable as an optional last param.

reducer callable it will transform 2 steps algo (populating the groups and then
reducing) to become a single step. but if it is not provided, the function
returns the member of the group (same as example above).


old way:

// step-1
$group = [];
foreach ($persons as $p) {/*...*/}

// step-2
foreach ($group as &$eduGroup) {
foreach ($eduGroup as &$sexGroup) {
$sumWeight = 0;
foreach ($sexGroup as $item) {
$sumWeight += $item->weight;
}
$sexGroup = $sumWeight / count($sexGroup);
}
}

output:

$groups = [
"elementary" => [
"female" => 40,
"male" => 50,
],
"highschool" => [
"female" => 60,
"male" => 65,
],
...
];

new way:

array_column_group(
$group,
["education", "sex", MORE_SUBGRUP],
fn ($item, $i, $cumm) => ($item->weight + ($cumm * $i)) / ($i + 1)
);


the signature:

// array_column_group signature
array_column_group(
array|object $array,
int|string|array $index_key,
callable $reducer = null
)

// callable signature
fn (array|object $item, int|string $index, mixed $cummulative): mixed

note:
- $index_key can be in the form `["education", ...]` or `"education"`.
- or even `[0, 1, ...]` or `0`.

So virtually, `array_column_group` is a hybrid between `array_column` and
`array_walk` (or other function with a funny name which I don't remember).

Best Regards
Hendra Gunawan.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php



Re: [PHP-DEV] Re: Array comparison works completely different than documented?

2021-12-04 Thread Jordan LeDoux
On Wed, Dec 1, 2021 at 7:00 AM Christoph M. Becker 
wrote:

>
> The point is that $a > $b is actually checking whether $b <= $a.  This
> is fine for ordered values, but these arrays are not orderable
> (according to PHP's comparison).  That might indeed not be documented in
> the PHP manual (the language specification appears to be abandoned anyway).
>
>
Are there any tests that capture this? As part of my operator overload RFC
I'm breaking out > into its own opcode instead of a reordered <=, so this
may actually be "fixed" by this. However, I haven't noticed any array
related comparison tests failing in my builds.

Jordan


[PHP-DEV] Re: [VOTE] Migrating to GitHub issues

2021-12-04 Thread Nikita Popov
On Sat, Nov 20, 2021 at 11:37 AM Nikita Popov  wrote:

> Hi internals,
>
> I've opened voting on https://wiki.php.net/rfc/github_issues. The vote
> closes 2021-12-04.
>
> Please see https://externals.io/message/116302 for the RFC discussion,
> and https://externals.io/message/114300 for the pre-RFC discussion.
>

The RFC has been accepted with 41 votes in favor and 4 against.

Regards,
Nikita


Re: [PHP-DEV] RFC [Discussion] array_column results grouping

2021-12-04 Thread Marco Pivetta
Gonna vote `no` on this: please design new/dedicated functions, rather than
expanding optional parameters.

On Sat, 4 Dec 2021, 10:57 Hassan Ahmed, <7sno...@gmail.com> wrote:

> Hello, this is a proposal to upgrade the functionality of
> `array_column` to return an indexed array for all of the matched data
> instead of a single result.
>
> the RFC had been created here
> https://wiki.php.net/rfc/array_column_results_grouping and the PR is
> in here https://github.com/php/php-src/pull/7698.
>
> However, I am not sure, if we need to add a fourth parameter to the
> function to change its behavior or we can add it as a new function,
> something like `array_column_group` or any other name.
>
> Regards,
> Hassan.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>


[PHP-DEV] RFC [Discussion] array_column results grouping

2021-12-04 Thread Hassan Ahmed
Hello, this is a proposal to upgrade the functionality of
`array_column` to return an indexed array for all of the matched data
instead of a single result.

the RFC had been created here
https://wiki.php.net/rfc/array_column_results_grouping and the PR is
in here https://github.com/php/php-src/pull/7698.

However, I am not sure, if we need to add a fourth parameter to the
function to change its behavior or we can add it as a new function,
something like `array_column_group` or any other name.

Regards,
Hassan.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php