【问题标题】:Turn one dimensional array with string path into multi dimensional json object将具有字符串路径的一维数组转换为多维 json 对象
【发布时间】:2014-05-14 11:25:16
【问题描述】:

我正在尝试解析一些数据,但我自己似乎无法弄清楚。

我尝试过使用递归并得到了大致的想法,但我似乎无法让数组索引正确排列。这是我目前所拥有的:

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;



}

我试图读取下一个路径数据以检查它是否在同一个数组索引内,但我无法让它工作。我知道我把它复杂化了,但似乎没有任何简单的方法可以做到这一点......

【问题讨论】:

    标签: php json recursion multidimensional-array


    【解决方案1】:

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

    // 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
             }
        }
    }
    

    希望这会有所帮助:)

    【讨论】:

    • 感谢您的努力。这确实解决了手头的问题!再次感谢
    猜你喜欢
    • 2014-09-23
    • 2021-01-17
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    相关资源
    最近更新 更多