将父树对象/数组转换为平面数组

时间:2015-06-26 14:41:54

标签: php arrays

我很难找到相关信息。有很多关于采用平面阵列和创建父树的信息,但无法逆转它,特别是当你不确定它有多深时。这就是我所拥有的:

array(
"id"=> "4",
"name"=> "online",
"safe_name"=> "online",
"drive_id"=> "1",
"parent_id"=> "3",
"created_at"=> "2015-06-24 14:06:10",
"updated_at"=> "2015-06-24 14:06:10",
"type"=> "folder",
"parents"=> array(
    "id"=>"3",
    "name"=>"dam12",
    "safe_name"=>"dam12",
    "drive_id"=>"1",
    "parent_id"=>2,
    "created_at"=>"2015-06-24 14:06:10",
    "updated_at"=>"2015-06-24 14:06:10",
    "type"=>"folder",
    "parents"=> array(
        "id"=> "2",
        "name"=> "Course Materials",
        "safe_name"=> "coure_materials",
        "drive_id"=> "1",
        "parent_id"=> NULL,
        "created_at"=> "2015-06-24 14:06:10",
        "updated_at"=> "2015-06-24 14:06:10",
        "type"=> "folder",
        "parents"=>array()
    )
)

我想要的是:

array(
    array(
        "id"=> "2",
        "name"=> "Course Materials",
        "safe_name"=> "coure_materials",
        "drive_id"=> "1",
        "parent_id"=> NULL,
        "created_at"=> "2015-06-24 14:06:10",
        "updated_at"=> "2015-06-24 14:06:10",
        "type"=> "folder"
    ),
    array(
        "id"=>"3",
        "name"=>"dam12",
        "safe_name"=>"dam12",
        "drive_id"=>"1",
        "parent_id"=>2,
        "created_at"=>"2015-06-24 14:06:10",
        "updated_at"=>"2015-06-24 14:06:10",
        "type"=>"folder"
    ),
    array(
        "id"=> "4",
        "name"=> "online",
        "safe_name"=> "online",
        "drive_id"=> "1",
        "parent_id"=> "3",
        "created_at"=> "2015-06-24 14:06:10",
        "updated_at"=> "2015-06-24 14:06:10",
        "type"=> "folder",
    )
)

我试图实现更多的道路。我有什么选择?实现这一目标的最佳方法是什么?

安德鲁给了我一个密切的解决方案,我需要做一些调整,因为我只得到树中的最后一项。这是我的代码,其他一切都在安德鲁的解释中解释。

if(is_array($parents)){
        foreach ($parents as $key => $parent) {
            if(isset($parent->parents)){
                if(is_array($parent->parents)){
                    $this->formatParents($parent->parents);
                }
                array_push($this->flat_parents, $parent);
            }else{
                array_push($this->flat_parents, $parent);
            }
        } 
    }

1 个答案:

答案 0 :(得分:1)

它将递归地收集未确定深度的数组中的所有值。

$test = array(
    "id"=> "4",
    "name"=> "online",
    "safe_name"=> "online",
    "drive_id"=> "1",
    "parent_id"=> "3",
    "created_at"=> "2015-06-24 14:06:10",
    "updated_at"=> "2015-06-24 14:06:10",
    "type"=> "folder",
    "parents"=> array(
        "id"=>"3",
        "name"=>"dam12",
        "safe_name"=>"dam12",
        "drive_id"=>"1",
        "parent_id"=>2,
        "created_at"=>"2015-06-24 14:06:10",
        "updated_at"=>"2015-06-24 14:06:10",
        "type"=>"folder",
        "parents"=> array(
            "id"=> "2",
            "name"=> "Course Materials",
            "safe_name"=> "coure_materials",
            "drive_id"=> "1",
            "parent_id"=> NULL,
            "created_at"=> "2015-06-24 14:06:10",
            "updated_at"=> "2015-06-24 14:06:10",
            "type"=> "folder",
            "parents"=>array()
        )
    )
);

class ItEasierWithAClass
{
    private $array_needed = array();

    public function getValues($array)
    {
        foreach($array as $key => $value)
        {
            if(is_array($value))
            {
                $this->getValues($value);
            }
            else
            {
                $this->array_needed[$array['id']][$key] = $value;
            }
        }
    }

    public function getArray()
    {
        return $this->array_needed;
    }
}


$test1 = new ItEasierWithAClass;

$test1->getValues($test);
echo '<pre>';
print_r($test1->getArray());

输出:

Array
(
    [4] => Array
        (
            [id] => 4
            [name] => online
            [safe_name] => online
            [drive_id] => 1
            [parent_id] => 3
            [created_at] => 2015-06-24 14:06:10
            [updated_at] => 2015-06-24 14:06:10
            [type] => folder
        )

    [3] => Array
        (
            [id] => 3
            [name] => dam12
            [safe_name] => dam12
            [drive_id] => 1
            [parent_id] => 2
            [created_at] => 2015-06-24 14:06:10
            [updated_at] => 2015-06-24 14:06:10
            [type] => folder
        )

    [2] => Array
        (
            [id] => 2
            [name] => Course Materials
            [safe_name] => coure_materials
            [drive_id] => 1
            [parent_id] => 
            [created_at] => 2015-06-24 14:06:10
            [updated_at] => 2015-06-24 14:06:10
            [type] => folder
        )

)

它不是世界上最漂亮的东西,但它完成了工作。