【问题标题】:How can I flatten this result set to have the parents and children as all one level?我怎样才能展平这个结果集以使父母和孩子都成为一个级别?
【发布时间】:2014-11-21 20:02:36
【问题描述】:

我正在尝试展平此数组,以使子级与父级处于同一级别。我已经尝试过其他 StackOverflow 问题的解决方案,例如 thisthis,但无济于事。有什么想法吗?

(int) 3 => object() {

    "new" => false,
    "properties" => [
        "id" => (int) 180,
        "name" => "Herp de Derp",
        "parent_id" => (int) 169,
        "lft" => (int) 16,
        "rght" => (int) 19,
        "children" => [
            (int) 0 => object(App\Model\Entity\Team) {

                "new" => false,
                "properties" => [
                    "id" => (int) 188,
                    "name" => "dood dood",
                    "parent_id" => (int) 180,
                    "lft" => (int) 17,
                    "rght" => (int) 18,
                    "children" => []
                ],
                "repository" => "Teams"
            }
        ]
    ],
    "repository" => "Teams"
},

这应该适用于孙子、曾孙等,而不仅仅是父母和一个孩子。

【问题讨论】:

  • 您正在查看数组问题但您的数据是一个对象,您是否尝试过转换为数组,然后在其上运行其他想法
  • 是的,我在上面运行'toArray'(CakePHP 3 命令),它应该已经转换了它,所以我假设调试只是转换之前的对象表示。为了确保,我会尝试快速投射。

标签: php arrays object flatten


【解决方案1】:

最终,如果您想从未知的树深度将其展平,则需要递归。我在一个文件(“test.json”)中模拟了一个更广泛的示例对象示例:

{
    "new": false,
    "properties": {
        "children": [
            {
                "new": false,
                "properties": {
                    "children": [],
                    "id": 188,
                    "lft": 17,
                    "name": "dood dood",
                    "parent_id": 180,
                    "rght": 18
                },
                "repository": "Teams"
            },
            {
                "new": false,
                "properties": {
                    "children": [],
                    "id": 182
                },
                "repository": "Teams"
            }
        ],
        "id": 180,
        "lft": 16,
        "name": "Herp de Derp",
        "parent_id": 169,
        "rght": 19
    },
    "repository": "Teams"
}

我在PHP中创建的一个示例程序来枚举,如下所示:

<?php
// $x is the JSON representation of this object (quick for scaffolding purposes)
// $y is the object representation
$x = file_get_contents("test.json");
$y = json_decode($x);

// $memo is our final product, an array that is written to as we traverse the object's children
$memo = [];

// Using recursive closure to walk the object/children and append to $memo
$z = function($n) use (&$z, &$memo) {
    // Build clone $o (sans children) of $n:
    $o = new stdClass;
    $o->new = $n->new;
    $o->repository = $n->repository;
    $o->properties = [];

    $children = [];
    foreach($n->properties as $k => $v) {
        if($k == "children") {
            // Do children afterward, to ensure current object
            // appears in array before children
            $children = $v;
            continue;
        } else {
            $o->properties[$k] = $v;
        }
    }
    array_push($memo, $o);
    array_walk($children, $z);
};
$z($y);
print_r($memo);

$z 是我的递归函数,出于偏好,我选择将其实现为闭包。我首先分配一个空数组 ($memo),然后通过首先添加当前对象来填充该数组,然后在事后添加对象的子对象。当它遍历孩子时,递归函数调用它自己的孩子,并在必要时依此类推。

通过以上,我的输出显示为:

Array
(
    [0] => stdClass Object
        (
            [new] => 
            [repository] => Teams
            [properties] => Array
                (
                    [id] => 180
                    [name] => Herp de Derp
                    [parent_id] => 169
                    [lft] => 16
                    [rght] => 19
                )

        )

    [1] => stdClass Object
        (
            [new] => 
            [repository] => Teams
            [properties] => Array
                (
                    [id] => 188
                    [name] => dood dood
                    [parent_id] => 180
                    [lft] => 17
                    [rght] => 18
                )

        )

    [2] => stdClass Object
        (
            [new] => 
            [repository] => Teams
            [properties] => Array
                (
                    [id] => 182
                )

        )

)

【讨论】:

    【解决方案2】:

    你想要的是将对象转换为数组:

    $arr = (array) $obj;
    

    对于json对象使用json_decode

    $arr = json_decode(json_encode($obj), true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多