将具有字符串路径的一维数组转换为多维json对象

时间:2014-04-02 22:02:33

标签: php json recursion multidimensional-array

我正在尝试解析一些数据,但似乎无法自己解决这个问题。

我尝试过使用递归并获得了一般性的想法,但我似乎无法让数组索引正确排列。以下是我到目前为止的情况:

public function reverse() {

$reversedValues= array();
foreach ( $this->sorted as $key=>$value ) {

    array_push( $this->reversedPaths ,$key  );
    array_push( $reversedValues , $value ); 
    //print_r($this->reversed);
    echo "<br />";

}
$this->reversed = $this->stringToArray($this->reversedPaths[0] , $reversedValues);
var_dump($this->reversed);
return  json_encode($this->reversed , false);
}

private function stringToArray($path , $values , $count = 0) {

$separator = '/';

$pos = strpos($path, $separator);

if ($pos === false) {
    //check for numbers so we know to add those to an json object
    if (is_numeric($path)) {
        //add it to the parent array of values...
    } 

    $reversed = array(
        $path => $values[$count],
    );

    return $reversed;
}

$key = substr($path, 0, $pos);
$path = substr($path, $pos + 1);


$reversed[$key] = $this->stringToArray($path  ,  $values , $count);
$count++;
//read in next path string
if (array_key_exists( $count ,$this->reversedPaths)) {

     $currentpos = strpos($this->reversedPaths[$count],  $path.$separator);
     $currentPath = substr($this->reversedPaths[$count], $currentpos );
     $nextpos = strpos($currentPath,  $separator);
     if ($nextpos === false) {

     } else {
         $nextPath = substr($currentPath, $nextpos + 1);
         $nextKey = substr($nextPath, 0, $nextpos);
         echo $nextKey;
         echo $nextPath;
        // echo $nextKey;
        //if this key equals first value of next path dont return but process recurssion again on it

        if ($nextKey !== false  ) {

            $reversed[$key][$nextKey] = $this->stringToArray($nextPath  ,  $values , $count);
        }
    }
} else {

}

return $reversed;



}

我试图在下一个路径数据中读取它以检查它是否在同一个数组索引中,但我无法让它工作。我知道我过度复杂了,但似乎没有任何简单的方法可以实现这一目标......

1 个答案:

答案 0 :(得分:2)

我对此有所了解。根据您提供的详细信息,您似乎正在尝试创建一个树,如目录结构:键中的每个/分隔字符串代表一个“深度”。我找到的解决方案是为每个元素创建一个多维数组,将当前键解析为级别,并递归地将结果合并/替换为主“树”数组。这就是我所拥有的:

// original JSON string

$json = '{
    "one/two": 3,
    "one/four/0": 5,
    "one/four/1": 6,
    "one/four/2": 7,
    "eight/nine/ten": 11
}';

// convert to a PHP multidimensional array

$array = json_decode($json, true);

// init an array to hold the final result

$tree = [];

// iterate over the array of values
// explode the key into an array 'path' tokens
// pop each off and build a multidimensional array
// finally 'merge' the result into the result array

foreach ($array as $path => $value) {
    $tokens = explode('/', $path);
    while (null !== ($key = array_pop($tokens))) {
        $current = [$key => $value];
        $value = $current;
    }
    $tree = array_replace_recursive($tree, $value);
}

// show the result!

print_r(json_encode($tree, JSON_PRETTY_PRINT));

收率:

{
    'one': {
        'two': 3,
        'four': [5, 6, 7]
    },
    'eight': {
        'nine': {
            'ten':11
         }
    }
}

希望这会有所帮助:)