$tags = ...array_column($arr, 'tags');
$merged_tags = array_merge($tags);
$unique_tags = array_unique($merged_tags);
$tag_values = array_values($unique_tags);
It also makes it easier to inspect the values after each step. $tags = ...array_column($arr, 'tags');
$merged_tags = array_merge($tags);
$unique_tags = array_unique($merged_tags);
$tag_values = array_values($unique_tags);
It also makes it easier to inspect the values after each step.With PHP allowing variable initialization in one branch but not the other, and continuing execution by default when an undeclared variable is passed, declaring more variables can lead to an annoying class of bugs that would require significant (breaking) changes to the core language to completely eliminate.
It's much easier skim with the pipe operator and it's more robust too (for example reordering is a pain with variables, it's easy to introduce errors).
One way this is prevented in PHP is just using functions. But then you have functions just for the sake of scope, which isn't really what they're for. That introduces other annoyances.
Preferably you should also be sure that the functions are compatible with the data type going in and only rarely have to break it to dump data mid-chain. If you expect that kind of erroring it's likely a builder-chain with -> is a better alternative and do logging in the methods.
Readability is mostly matter of habit. One reads easily what he/she is used to read.
They can be. It depends on the language, interpreter, compiler, and whether you do anything with those intermediate variables and the optimiser can get rid of them.
That's like saying someone would use this:
$result = $arr |> fn($x) => array_column($x, 'tags') |> fn($x) => array_merge(...$x) |> array_unique(...) |> array_values(...)
which is harder to reason about than the nested functions. array_values( array_unique( array_merge( ...array_column($arr, 'tags') ) ) );
or array_values(
array_unique(
array_merge(
...array_column($arr, 'tags')
)
)
); $result = $arr
|> fn($x) => array_column($x, 'tags')
|> fn($x) => array_merge(...$x)
|> array_unique(...)
|> array_values(...)
vs array_values(
array_unique(
array_merge(
...array_column($arr, 'tags')
)
)
);
With pipes you have linear sequence of data transformations. With nested function calls you have to start with innermost function and proceed all the way top the outermost layer.