I have an array having unique 'table_id' that will find 'parent_id' and goes under parent index.
My Array:

Array
(
    [0] => Array
        (
            [table_id] => 7
            [table_name] => Macro
            [parent_table] => 1
        )

    [1] => Array
        (
            [table_id] => 4
            [table_name] => Dise
            [parent_table] => 7
        )

    [2] => Array
        (
            [table_id] => 5
            [table_name] => Cox
            [parent_table] => 7
        )

    [3] => Array
        (
            [table_id] => 6
            [table_name] => Ripo
            [parent_table] => 4
        )

)

Sample output:

Array
(
    [1] => Array
        (
            [table_id] => 7
            [table_name] => Macro
            [parent_table] => 1
            [7] => Array
                (
                    [table_id] => 4
                    [table_name] => Dise
                    [parent_table] => 7
                    [4] => Array
                        (
                            [table_id] => 6
                            [table_name] => Ripo
                            [parent_table] => 4
                        )

                )

            [7] => Array
                (
                    [table_id] => 5
                    [table_name] => Cox
                    [parent_table] => 7
                )

        )

)

Please give some idea to build this.

Try this;

$menuList=array();
        $parentId = 0;
        $result = mysql_query("SELECT * FROM    db_Table  WHERE (parent_table = {$parentId}) ;");
        $mainMenus = mysql_fetch_array($result);
        foreach($mainMenus as $mm) {
            array_push($menuList, $mm);

            $result = mysql_query("SELECT * FROM     db_Table  WHERE  parent_table = {$mm->table_id};");
            $subMenu = mysql_fetch_array($result);

            if(sizeof($subMenu)>0) {
                array_push($menuList, $subMenu);
            }
        }
        print_r($menuList);

This is a basic code. inside it check, if any row with parent id equal to parnt id .. repeate this query

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.