<?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);
You have javascript disabled. You will not be able to edit any code.