3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { public function bar($tags, $product_row) { $path_arr = []; foreach ($product_row['fields'] as $field) { //gets the path for this field like pathto/field $strpath = $tags[$field['id']]['str_path']; $paths = explode('/', $strpath); $this->stringKeyToMultArray($path_arr, $paths, $field['value']); } return json_encode($path_arr, JSON_PRETTY_PRINT); } protected function stringKeyToMultArray(&$newarr, $keys, $value) { if (count($keys) > 1) { $key = array_shift($keys); if ( !isset($newarr[$key]) || !is_array($newarr[$key])) { $newarr[$key] = array(array()); } $this->stringKeyToMultArray($newarr[$key][0], $keys, $value); } else { $newarr[array_shift($keys)] = $value; } } } $foo = new Foo; $tags = array( 1 => array('str_path' => 'product_title'), 2 => array('str_path' => 'ASIN'), 3 => array('str_path' => 'codes/type'), 4 => array('str_path' => 'codes/number'), 5 => array('str_path' => 'quantity') ); $product_row = array('fields' => array( array('id' => 1, 'value' => 'Test Title'), array('id' => 2, 'value' => '1234567890'), array('id' => 3, 'value' => 'UPC'), array('id' => 4, 'value' => '030878249270'), array('id' => 3, 'value' => 'UPC'), )); echo $foo->bar($tags, $product_row);
Output for git.master, git.master_jit, rfc.property-hooks
{ "product_title": "Test Title", "ASIN": "1234567890", "codes": [ { "type": "UPC", "number": "030878249270" } ] }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
159.02 ms | 405 KiB | 5 Q