drkug66408 2015-06-26 14:41
浏览 32
已采纳

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

I'm having a difficult time finding information on this. There is plenty of information about taking a flat array and creating a parent tree, but no way to reverse it, especially when you are not sure how deep it goes. This is what I have:

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()
    )
)

)

What I am trying to get is:

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",
    )
)

I'm trying to achieve something more of a path. What are my options? What is the best way to achieve this?

SOLUTION Andrew gave me a close solution, I needed to do some tweaking as I was only getting the last item in the tree. here is my code, everything else is explained in Andrew's explanation.

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条回答 默认 最新

  • dounianluo0086 2015-06-26 14:45
    关注

    It will recursively gather all the values from an array of undetermined depth.

    $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());
    

    Output:

    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
            )
    
    )
    

    It's not the prettiest thing in the world, but it gets the job done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动