Php 将带有字符串路径的一维数组转换为多维json对象
我试图解析一些数据,但我自己似乎无法理解 我尝试过使用递归,并得到了一般的想法,但我似乎无法解决让数组索引排列正确的问题。以下是我到目前为止的情况:Php 将带有字符串路径的一维数组转换为多维json对象,php,json,recursion,multidimensional-array,Php,Json,Recursion,Multidimensional Array,我试图解析一些数据,但我自己似乎无法理解 我尝试过使用递归,并得到了一般的想法,但我似乎无法解决让数组索引排列正确的问题。以下是我到目前为止的情况: public function reverse() { $reversedValues= array(); foreach ( $this->sorted as $key=>$value ) { array_push( $this->reversedPaths ,$key ); array_push( $re
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;
}
public function reverse(){
$reversedValues=array();
foreach($this->排序为$key=>$value){
数组_push($this->reversedpath,$key);
数组_push($reversedValues,$value);
//打印($this->reversed);
回声“
”;
}
$this->reversed=$this->stringToArray($this->reversedPaths[0],$reversedValues);
var_转储($this->reversed);
返回json_encode($this->reversed,false);
}
私有函数stringToArray($path,$value,$count=0){
$separator='/';
$pos=strpos($path,$separator);
如果($pos==false){
//检查数字,以便我们知道如何将这些数字添加到json对象中
if(是数值($path)){
//将其添加到值的父数组。。。
}
$reversed=数组(
$path=>$values[$count],
);
返回$REVERSE;
}
$key=substr($path,0,$pos);
$path=substr($path$pos+1);
$reversed[$key]=$this->stringToArray($path,$values,$count);
$count++;
//读入下一个路径字符串
如果(数组\键\存在($count,$this->reversedPaths)){
$currentpos=strpos($this->reversedPaths[$count],$path.$separator);
$currentPath=substr($this->reversedPaths[$count],$currentpos);
$nextpos=strpos($currentPath,$separator);
如果($nextpos==false){
}否则{
$nextPath=substr($currentPath,$nextpos+1);
$nextKey=substr($nextPath,0,$nextpos);
echo$nextKey;
echo$nextPath;
//echo$nextKey;
//若该键等于下一个路径的第一个值,则不返回,而是在其上再次处理递归
如果($nextKey!==false){
$reversed[$key][$nextKey]=$this->stringToArray($nextPath,$values,$count);
}
}
}否则{
}
返回$REVERSE;
}
我试图读入下一个路径数据以检查它是否在同一个数组索引中,但我就是无法让它工作。我知道我把事情复杂化了,但似乎没有任何简单的方法来完成这件事……我在这方面遇到了困难。根据您提供的详细信息,看起来您正在尝试创建一个树,类似于目录结构:键中的每个
/
分隔字符串表示一个“深度”。我找到的解决方案是为每个元素创建一个多维数组,将当前键解析为级别,并递归地将结果合并/替换为主“树”数组。这就是我所拥有的:
// 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
}
}
}
希望这有帮助:)感谢您的努力。这确实解决了眼前的问题!再次感谢