On Fri, May 29, 2026 at 6:07 PM Larry Garfield <[email protected]>
wrote:

>
> My first thought is that the current code, which assigns potentially null
> values, works fine if you throw an array_filter() at it at the end.
>
> $array = [
>     'field1' => $param1,
>     'field2' => $param2,
>     'field3' => $param3_which_is_null,
> ];
>
> $a = array_filter($array);
> // $a now omits the null fields
>
> My second thought is that this is yet another reason why using associative
> arrays as if they were a data structure is wrong and should be avoided;
> just use a class and everything will be fine.  If you need to serialize it
> later, there's many serializers on the market if JsonSerialize isn't
> sufficient.
>
> So this doesn't seem like a worthwhile addition to me.
>
> --Larry Garfield
>

While I do agree, I found myself using arrays because I don't always want
to depend on an external library for something as simple as json_encode,
nor do I want to create 5 different files for 5 different objects for a
nested structure with the current PSR standards. One of my use-cases is an
external GQL library where the data is sent as json, and with dozens of
queries/mutations that would easily lead to 50~60 extra classes _just_ for
serialization purposes. I've found myself writing code like this more
frequently:
```
return [
    'key1' => $this->value1,
    'key2' => $this->value2,
    ...($this->value3 ? ['key3' => $this->value3] : []),
];
```
It's not always json, sometimes it's also DB parameters where binding
`null` can mean something different than not having the key. Same for data
that ends up converted to XML where an empty element means something
different than an absent element. I'm not specifically for or against this
change, I just want to note that the presence of a key with null value vs
an absent key can make a difference, whether you own the consuming code or
not. This particular RFC would add syntactic sugar that I would most
certainly use if it was available to me.

I also want to note that array_filter is a falsey check by default, and
feels like overhead just to remove what you've just added.

Reply via email to