0

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
  • Check it now, should work fine. Commented Jun 29, 2015 at 15:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, but this is returning a nested array. I need it to be flat.
If by nested you mean [ 0 => 'somevalue', 1 => 'anothervalue' ...] then yes. All arrays are like that, even when creating an array yourself, [0, 1, 2...]. Even if the keys are not specified, they will exist.
No, what I mean is its coming back like my example above [array[array[array[array]]]]
That's strange, what version of PHP are you running? If it's below 5.3.0, you can't have anonymous functions. But still, it should have error'd out. Lemme see what's wrong. I'll reply in 5 mins max.
Seems to be working perfectly for me. 6 levels nested array. There must be something else wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.