1

I want to format array with Tree structure like below in PHP

TITLE 1
-- SUB TITLE 1-1
---- SUB TITLE 1-2
TITLE 2
-- SUB TITLE 2-1

Array structure :

Array
(
    [0] => Array
        (
            [id] => 1
            [sub_id] => 1
            [title] => TITLE 1
            [url] => www.title1.com
        )

    [1] => Array
        (
            [id] => 4
            [sub_id] => 4
            [title] => TITLE 2
            [url] => www.title2.com
        )

    [2] => Array
        (
            [id] => 2
            [sub_id] => 1
            [title] => SUB TITLE 1-1
            [url] => www.subtitle1.com
        )

    [3] => Array
        (
            [id] => 3
            [sub_id] => 2
            [title] => SUB TITLE 1-2
            [url] => www.subtitle2.com
        )

    [4] => Array
        (
            [id] => 5
            [sub_id] => 4
            [title] => SUB TITLE 2-1
            [url] => www.subtitle2.com
        )

)

I have no idea how it should be done.

4
  • 2
    You should have a recursive function that harvest the array for each element and looking for it's children. Commented Apr 22, 2015 at 6:58
  • It's somewhat unclear what exactly the result needs to be. Start here to get some idea: stackoverflow.com/a/8587437/476 Commented Apr 22, 2015 at 7:09
  • i had seen link but my case is different. @deceze Commented Apr 22, 2015 at 8:18
  • Can you explain the format? Do the id or sub_id values need to link up with the array keys? Commented Apr 22, 2015 at 8:24

1 Answer 1

2

Here you are my solution:

<?php
$data=Array
(
    0 => Array
        (
            'id' => 1,
            'sub_id' => 1,
            'title' => 'TITLE 1',
            'url' => 'www.title1.com',
        ),

    1 => Array
        (
            'id' => 4,
            'sub_id' => 4,
            'title' => 'TITLE 2',
            'url' => 'www.title2.com',
        ),

    2 => Array
        (
            'id' => 2,
            'sub_id' => 1,
            'title' => 'SUB TITLE 1-1',
            'url' => 'www.subtitle1.com',
        ),

    3 => Array
        (
            'id' => 3,
            'sub_id' => 2,
            'title' => 'SUB TITLE 1-2',
            'url' => 'www.subtitle2.com',
        ),

    4 => Array
        (
            'id' => 5,
            'sub_id' => 4,
            'title' => 'SUB TITLE 2-1',
            'url' => 'www.subtitle2.com',
        ),

);

function find_index(&$arr, $ind){
    if(empty($arr))
        return false;
    else {
        foreach($arr as &$aa){
            if($aa['id']==$ind['sub_id']) {
                $key=count($aa['sub_ar']);
                $aa['sub_ar'][$key]=$ind;
                $aa['sub_ar'][$key]['sub_ar']=array();
                return true;
            }
            else{
                $res=find_index($aa['sub_ar'],$ind);
                if($res)
                    return true;
            }
        }
        return false;
    }
}

$res=array();
foreach($data as $key=>$dat){
    if($dat['sub_id']==$dat['id']){
        $res[$key]=$dat;
        $res[$key]['sub_ar']=array();
    }
    else {
        $ret=find_index($res,$dat);
    }
}
?>
<pre><?php print_r($res); ?></pre>
Sign up to request clarification or add additional context in comments.

Comments

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.