3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $mapping = [ new Mapper(['jobname' => 'beruf']), new Mapper(['num_salary' => 'einkommen.anzahlGehaelter']), new Mapper(['netincome' => 'einkommen.netto']), new Mapper(['grossincome' => 'einkommen.brutto']) ]; $payload = [ 'beruf' => 'Maler', 'einkommen' => [ 'anzahlGehaelter' => 12, 'netto' => 51253, 'brutto' => 786522 ] ]; final class Mapper { private $key; private $name; public function __construct(array $mapping) { $this->key = key($mapping); $this->name = current($mapping); } public function extract(array $payload): array { foreach (explode('.', $this->name) as $name) { if (!array_key_exists($name, $payload)) { return []; } $payload = $payload[$name]; } return [ $this->key => $payload ]; } } $output = []; foreach ($mapping as $mapper) { $output = array_merge($output, $mapper->extract($payload)); } print_r($output);
Output for hhvm-3.15.4, 7.0.0 - 7.3.0rc6
Array ( [jobname] => Maler [num_salary] => 12 [netincome] => 51253 [grossincome] => 786522 )
Output for 5.6.0 - 5.6.30
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/ab2ls on line 30
Process exited with code 255.