3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ ['Template' => 'page.html5'], ['Template' => 'page2.html5'], ['Template' => 'page.html5'], ['Template' => 'page2.html5'], ['Template' => 'page.html5'], ['Template' => 'page2.html5'], ['id' => 27, 'table' => 'tl_custom', 'type' => 'text', 'data' => [ 'fragment' => 'example', 'previewId' => 1, 'isActive' => 1, ], ] ]; $newArr = array_unique(array_map(function ($el) { return $el['Template'] ?? serialize($el); }, $arr)); // restore the last element to array $last = array_key_last($newArr); $newArr[$last] = unserialize($newArr[$last]); echo '<pre>'; print_r($newArr); echo '</pre>';
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
<pre>Array ( [0] => page.html5 [1] => page2.html5 [6] => Array ( [id] => 27 [table] => tl_custom [type] => text [data] => Array ( [fragment] => example [previewId] => 1 [isActive] => 1 ) ) ) </pre>
Output for 7.2.0 - 7.2.33
Fatal error: Uncaught Error: Call to undefined function array_key_last() in /in/4kDiP:25 Stack trace: #0 {main} thrown in /in/4kDiP on line 25
Process exited with code 255.

preferences:
148.4 ms | 407 KiB | 5 Q