2016-09-16 54 views
0

我想展平一个物体。这是我到目前为止:保持秩序时展平多维物体

{ 
    "1": { 
     "id": 1, 
     "name": "parent", 
     "children": { 
      "4": { 
       "id": 4, 
       "name": "child1", 
       "parent": 1 
      }, 
      "5": { 
       "id": 5, 
       "name": "child2", 
       "parent": 1 
      } 
     } 
    }, 
    "2":{ 
     "id": 2, 
     "name": "parent2" 
    } 
} 

这就是我想要完成的。因此,保持相同的顺序,但扁平的对象:

{ 
    "1": { 
     "id": 1, 
     "name": "parent", 
    }, 
    "4": { 
     "id": 4, 
     "name": "child1", 
     "parent": 1 
    }, 
    "5": { 
     "id": 5, 
     "name": "child2", 
     "parent": 1 
    }, 
    "2": { 
     "id": 2, 
     "name": "parent2" 
    } 
} 

到目前为止我还没有找到解决方案。我尝试了一个没有太多成功的功能:

protected function _flattenObject($array) 
{ 
    static $flattened = []; 
    if(is_object($array) && count($array) > 0) 
    { 
     foreach ($array as $key => $member) { 
      if(!is_object($member)) 
      { 
       $flattened[$key] = $member; 
      } else 
      { 
       $this->_flattenObject($member); 
      } 
     } 
    } 
    return $flattened; 
} 

对我来说,困难的部分是保持相同的顺序(孩子低于其父母)。上面提到的函数也删除了所有的对象,并且几乎只保留了它的值,所以它并不是很成功。

希望在这里有人知道这个很好的解决方案。

顺便说一句,我想要这样的扁平结构的原因是因为我必须使用的系统,无法处理多维数组和对象。我仍然希望显示一个层次结构,这可能与我描述的扁平化结构有关,因为对象实际上也包含一个“级别”键,所以我可以给它们一些基于“级别”的填充,同时仍然显示在它们的下面家长。

编辑: JSON似乎不是有效的,所以我修改了一下。

+0

您的原始数据是指对象的数组,还是作为对象的对象是正确的? –

+0

@The One and Only ChemistryBlob确实是一个物体的对象。 –

+0

@Ryan Vincent JSON对象现在应该是有效的 –

回答

1

主要的问题似乎是你没有对递归函数的返回结果做任何事情。除非使用static的方法里面做了一些魔术,我不知道......

所以本节:

 if(!is_object($member)) 
     { 
      $flattened[$key] = $member; 
     } else 
     { 
      // What happens with the returned value? 
      $this->_flattenObject($member); 
     } 

可能应该更多这样的:

 if(!is_object($member)) 
     { 
      $flattened[$key] = $member; 
     } else 
     { 
      // Add the returned array to the array you already have 
      $flattened += $this->_flattenObject($member); 
     } 
+0

谢谢Jeroen,但目前它只返回最后一个对象,id 2是确切的。我没有想到......奇怪。即使没有'+',所以有些东西变得非常错误。猜猜我必须先排除故障。一想到这一点,我会尽快回复你。 –

+0

我做了一些测试,不幸的是这个功能根本不起作用。在将子项移动到根级别时,保留对象似乎非常困难。要么它平整一切(所以我只有一个包含键和值的数组),或者它保持相同的结构,实际上什么都不做。 –

0

这里代码有效。它向对象添加一个字段“级别”,以表示它们原有层次结构中的深度级别。

<?php 

$obj = json_decode('[{ 
    "id": 1, 
    "name": "parent", 
    "children": [{ 
     "id": 4, 
     "name": "child1", 
     "parent": 1 
    }, { 
     "id": 5, 
     "name": "child2", 
     "parent": 1 
    }] 
}, { 
    "id": 2, 
    "name": "parent2" 
}]'); 


function _flattenRecursive($array, &$flattened, &$level) 
{ 
    foreach ($array as $key => $member) { 
     $insert = $member; 
     $children = null; 
     if (is_array($insert->children)) { 
      $children = $insert->children; 
      $insert->children = array(); 
     } 
     $insert->level = $level; 
     $flattened[] = $insert; 
     if ($children !== null) { 
      $level++; 
      _flattenRecursive($children, $flattened, $level); 
      $level--; 
     } 
    } 
} 

function flattenObject($array) 
{ 
    $flattened = []; 
    $level = 0; 

    _flattenRecursive($array, $flattened, $level); 

    return $flattened; 
} 

$flat = flattenObject($obj); 

var_dump($flat); 

?> 
+0

谢谢,但你检查一个数组有点奇怪,而在我的例子中,json只是一个对象。 –