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