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 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
{ "product_title": "Test Title", "ASIN": "1234567890", "codes": [ { "type": "UPC", "number": "030878249270" } ] }
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
179.8 ms | 407 KiB | 5 Q