3v4l.org

run code in 300+ PHP versions simultaneously
<?php $yaml = [ 'components' => [ [ 'cachet' => [ 'componentgroup' => 'Bar', 'componentname' => 'Name1' ] ], [ 'cachet' => [ 'componentgroup' => 'Bar', 'componentname' => 'Name2' ] ] ] ]; $yamlMap = []; foreach ($yaml['components'] as $yamlComponent) { $key = $yamlComponent['cachet']['componentgroup']; $value = $yamlComponent['cachet']['componentname']; // Lets initialize the key to be an array so that we may collect multiple values if(!array_key_exists($key, $yamlMap)) { $yamlMap[$key] = []; } // lets add the value to the map under the key $yamlMap[$key][] = $value; } var_dump($yamlMap);

preferences:
41 ms | 404 KiB | 5 Q