I was happy to get that thing working in PHP 7.4, but now it's broken again in PHP 8.1.14.
The task is to sort an array of pages by their title, but in a simplified way. Leading non-letters shall be ignored when sorting.
This is a sample code:
$pages = [
'a' => ['title' => 'A'],
'c' => ['title' => 'C'],
'b' => ['title' => 'B'],
'n' => ['title' => '.N'],
];
array_multisort(
$pages,
SORT_ASC,
SORT_STRING,
array_map(fn($page) =>
preg_replace('_^[^0-9a-z]*_', '', strtolower($page['title'])
), $pages)
);
echo implode(', ', array_map(fn($page) => $page['title'], $pages));
It sorts to this output in PHP 7.4, nothing in the error log:
A, B, C, .N
In PHP 8.1, I get this error instead:
ErrorException: Array to string conversion in ...\index.php:30
Stack trace:
#0 [internal function]: {closure}()
#1 ...\index.php(30): array_multisort()
I don't understand what the problem is. What has been changed here for PHP 8 and what can I do to fix the code again?
Update/clarification:
In PHP 7.4, there is a "Notice" with the text "Array to string conversion". In PHP 8.1 this has been changed to a "Warning". I treat warnings as errors so it fails in my case.
That doesn't change that PHP thinks my code is wrong and I don't have a clue why.
"array()". Resolve this problem and your code will probably also work in PHP 8. Warnings are there for a reason, ignore them at your own peril.