3v4l.org

run code in 300+ PHP versions simultaneously
<?php $user = [ 'id' => 1, 'name' => 'Ivan Ivanov', 'role' => 'developer', 'salary' => 100 ]; $apiTemplatesSet1 = [ '/api/items/%id%/%name%', '/api/items/%id%/%role%', '/api/items/%id%/%salary%' ]; class Invoker { private $data; public function __construct(array $data) { $this->data = $data; } public function __invoke(array $key) { return $this->data[$key[1]]; #echo '<pre>' . print_r($input, true); #return array_map([$this, 'replace'], $input); } private function replace(string $key): string { return $this->data[trim($key, '%')]; } } $function = new Invoker($user); $result = array_map(function ($key) use ($function) { return preg_replace_callback('#%(.*)%#isU', $function, $key); } , array_values($apiTemplatesSet1)); echo '<pre>' . print_r($result, true);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<pre>Array ( [0] => /api/items/1/Ivan Ivanov [1] => /api/items/1/developer [2] => /api/items/1/100 )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <pre>Array ( [0] => /api/items/1/Ivan Ivanov [1] => /api/items/1/developer [2] => /api/items/1/100 )

preferences:
165.18 ms | 402 KiB | 177 Q