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);

preferences:
28.64 ms | 402 KiB | 5 Q