3v4l.org

run code in 200+ 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): string { return $this->data[$key[1]]; } } $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);
based on 5VWU0
Output for 7.2.0 - 7.4.2
<pre>Array ( [0] => /api/items/1/Ivan Ivanov [1] => /api/items/1/developer [2] => /api/items/1/100 )