Jump to content

how can i turn an arry of objects into a multidimensional array


emehrkay

Recommended Posts

say i have, but with about 50 objects

 

Array
(
    [0] => stdClass Object
        (
            [navigation_id] => 1
            [parent_nav_id] => 0
            [display_name] => Home
            [link_id] => top:home
            [nav_link] => ./home.php
            [date_created] => 2007-03-09 16:51:26
            [date_modified] => 2007-03-09 16:51:26
        )

    [1] => stdClass Object
        (
            [navigation_id] => 2
            [parent_nav_id] => 0
            [display_name] => Music
            [link_id] => top:music
            [nav_link] => #
            [date_created] => 2007-03-09 16:51:26
            [date_modified] => 2007-03-09 16:51:26
        )
)

 

each have a parent_nav_id. what i want to do is loop through this array and create other arrays grouped by parent id. then from there i want to take all of those arrays and make it a multidim based on the parent/child relationship

 

so id have this

 

array(top => array(all of the arrays with 0 for parent), 'mid' => array(all of the arrays whose parent is one in the top), 'bot' => array(all of the arrays whose parent is in mid)) etc etc

 

my problem is creating the new arrays and storing them so that i can later group them. any suggestions?

 

thanks

Link to comment
Share on other sites

This is fun stuff, If you can create (write out your own) array definition that would be helpful than I can show you how to do it automatically.

 

tarun, a multi-dimensional array is an array within an array, for example:

 

$multDimArr = array("array1" => array("multiArr" => 1));

 

That is a multi-dimensional array. To access "multiArr" you would do this:

 

$multDimArr["array1"]["multiArr"] which contains "1".

 

--FrosT

Link to comment
Share on other sites

frost, i already have the structure defined, i created it manually. What i am trying to do now is modify a result set from a query to fit that mold.

 

my first post shows my result

 

here is the final structure. after it is in this format, i create unordered lists based on relationships [that is already done]

 

i thought that my array game was pretty solid, i guess i need to work on it a lot

 

thanks for the help

 

 

Array
(
    [top] => Array
        (
            [0] => Array
                (
                    [id] => top:top
                    [Home] => Array
                        (
                            [id] => top:home
                            [link] => home.php
                        )

                    [Music] => Array
                        (
                            [id] => top:music
                            [link] => javascript:void(0);
                        )

                    [Members] => Array
                        (
                            [id] => top:mem
                            [link] => javascript:void(0);
                        )

                    [Discussions] => Array
                        (
                            [id] => top:dis
                            [link] => javascript:void(0);
                        )

                    [Logout] => Array
                        (
                            [id] => top:logout
                            [link] => login.php?action=logout
                        )

                )

        )

    [mid] => Array
        (
            [members] => Array
                (
                    [id] => mid:mem
                    [users] => Array
                        (
                            [id] => mid:usr
                            [link] => javascript:void(0);
                        )

                    [Top 8] => Array
                        (
                            [id] => mid:top8
                            [link] => javascript:void(0);
                        )

                )

            [music] => Array
                (
                    [id] => mid:music
                    [Artists] => Array
                        (
                            [id] => mid:artists
                            [link] => javascript:void(0);
                        )

                    [Genres] => Array
                        (
                            [id] => mid:genres
                            [link] => javascript:void(0);
                        )

                    [Albums] => Array
                        (
                            [id] => mid:albums
                            [link] => javascript:void(0);
                        )

                )

            [discussion] => Array
                (
                    [id] => mid:dis
                    [search] => Array
                        (
                            [id] => mid:searchDis
                            [link] => javascript:void(0);
                        )

                    [Disp Holder] => Array
                        (
                            [id] => mid:holder
                            [link] => javascript:void(0);
                        )

                )

        )

    [bot] => Array
        (
            [members] => Array
                (
                    [id] => bot:usr
                    [search] => Array
                        (
                            [id] => bot:search
                            [link] => javascript:void(0);
                        )

                    [browse] => Array
                        (
                            [id] => bot:browse
                            [link] => javascript:void(0);
                        )

                    [Message] => Array
                        (
                            [id] => bot:message
                            [link] => javascript:void(0);
                        )

                )

            [top8] => Array
                (
                    [id] => bot:top8
                    [View] => Array
                        (
                            [id] => bot:view
                            [link] => javascript:void(0);
                        )

                )

            [artists] => Array
                (
                    [id] => bot:artists
                    [browse A-Z] => Array
                        (
                            [id] => bot:artBrowse
                            [link] => javascript:void(0);
                        )

                    [search] => Array
                        (
                            [id] => bot:artSearch
                            [link] => #
                        )

                )

            [genres] => Array
                (
                    [id] => bot:genres
                    [Country] => Array
                        (
                            [id] => bot:country
                            [link] => javascript:void(0);
                        )

                    [Rap] => Array
                        (
                            [id] => bot:rap
                            [link] => javascript:void(0);
                        )

                    [R & B] => Array
                        (
                            [id] => bot:rb
                            [link] => javascript:void(0);
                        )

                )

        )

)

Link to comment
Share on other sites

To be honest with you, I would love to see working code of this. I tried doing a few category recursion scripts with no luck. I can drilldown to the 3rd one but after that its all posh.

 

If you do find the answer please post it here as I would be interested in seeing the code.

 

--FrosT

Link to comment
Share on other sites

I tried with arrays and sub array but my recursive function struggled with the reference to the subarrays and insisted on passing copies instead.

 

So I took another approach and created a recursive "tree" class containing object reference and its children

 

(Note: PHP5 as v4 doesn't handle object references by default.)

 

<?php
class myObj {
    public $id;
    public $parent;
    public $name;
    
    function __construct ($id, $parent, $name)  {
        $this->id = $id;
        $this->parent = $parent;
        $this->name = $name;
    }
}

class myTree {
    public $obj;
    public $children;
    
    function __construct ($obj) {
        $this->obj = $obj;
        $this->children = array();
    }
    
    function addChild ($obj) {
        $this->children[] = new myTree($obj);
    }
    
    function printTree ($level) {
        if ($this->obj) {
            $indent = str_repeat('&nbsp', $level*5);
            echo $indent, $this->obj->id, ' : ', $this->obj->name, "<br>" ;
        }
        foreach ($this->children as $c) {
            $c->printTree($level+1);
        }
    }
}

/**
* test array (id, parent, name)
*/
$objArray = array(
   1 => new myObj (1, 0, 'a'),
        new myObj (2, 0, 'b'),
        new myObj (3, 0, 'c'),
        new myObj (4, 1, 'd'),
        new myObj (5, 2, 'e'),
        new myObj (6, 3, 'f'),
        new myObj (7, 1, 'g'),
        new myObj (8, 3, 'h'),
        new myObj (9, 2, 'j'),
        new myObj (10, 3, 'k'),
        new myObj (11, 1, 'l'),
        new myObj (12, 2, 'm'),
        new myObj (13, 3, 'n'),
        new myObj (14, 1, 'p'),
        new myObj (15, 3, 'q'),
        new myObj (16, 4, 'r'),
        new myObj (17, 4, 's'),
        new myObj (18, 5, 't'),
        new myObj (19, 16, 'u')
);

$results = new myTree(null);

foreach ($objArray as $obj) {
    /**
    * if parent is 0 add directly to root of tree 
    */
    if ($obj->parent == 0) {
        $results->addChild($obj);
    }
    /**
    * otherwise search each children array to if it contains the parent
    */
    else {
        add_to_children ($obj, $results->children);
    }
}

function add_to_children ($obj, $ancestor) {
    foreach ($ancestor as $tree) {
        /**
        * if parent found, add child
        */
        if ($tree->obj->id == $obj->parent) {
            $tree->addChild($obj);
        }
        /**
        *  otherwise search its children
        */
        else add_to_children($obj, $tree->children) ;
    }
} 

$results->printTree(0);


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.