SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast stationmaster's Avatar
    Join Date
    May 2003
    Location
    barcelona
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up How do you sort Multidimensional arrays ?[Resolved]

    I am building a wordpress plugin to create a hierarchical menu of categories with posts. I need to separate the different levels : level 0, level 1, level 2 etc.
    I have been reading, searching and testing for some time now. I hope a solution might be offered from
    the Sitepoint community or any tips or pointers to an appropriate tutorial. Thanx.

    The category array responce is:

    Code PHP:
    Array
    (
        [0] => stdClass Object
            (
                [count] => 2
                [term_id] => 9
                [name] => Galleries-level1
                [slug] => galleries-level1
                [parent] => 0
                [description] => 
            )
     
        [1] => stdClass Object
            (
                [count] => 3
                [term_id] => 10
                [name] => Galleries-level2
                [slug] => galleries-level2
                [parent] => 9
                [description] => 
            )
     
        [2] => stdClass Object
            (
                [count] => 3
                [term_id] => 11
                [name] => Galleries-level3
                [slug] => galleries-level3
                [parent] => 10
                [description] => 
            )
     
    etc...
    I reduce the category array to cats with:

    Code PHP:
    foreach( $categories as $cat )


    I can determine if a cat has a parent of level 0 with this:
    Code PHP:
    if ($cat->parent != 0)

    Which tells me that item [1] with term_id = 10 is the son of item [0] with term_id = 9 and parent = 0.

    Code PHP:
    foreach ($categories as $cat) 
    			  {
    					if ($cat->parent != 0) 
    					{
    					  array_push( $parents, $cat->parent );
    					}
    			  }
    The array parent returns all cats with a parent:
    Code PHP:
    Array
    (
        [0] => 10
        [1] => 11
        [2] => 12
    )
    What I need to do is determin if a category item is a grandson or greatgrandson recursively.

    Code PHP:
    foreach ($categories as $cat) 
    			  {
    					if ($cat->parent != 0) 
    					{
    					  array_push( $parent_1, $cat->parent );
    					  // echo "I am level 1 down";
    					}
    					if ($cat->parent->parent == 0)
    					//this is obviously wrong, placed to convey objective
    					{
    					array_push( $parent_2, ? );
    					//echo "I am level 2 down";
    					}
    					if ($cat->parent->parent->parent == 0)
    					//this is obviously wrong, placed to convey objective
    					{
    					array_push( $parent_3, ? );
    					//echo "I am level 3 down";
    					}
    			  }

    The category query:
    Code PHP:
    $catquery = "SELECT $wpdb->term_taxonomy.count as 'count',
    			$wpdb->terms.term_id, $wpdb->terms.name, $wpdb->terms.slug,
    			$wpdb->term_taxonomy.parent, $wpdb->term_taxonomy.description FROM
    			$wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id =
    			$wpdb->term_taxonomy.term_id AND $wpdb->terms.name != 'Blogroll' AND
    			$wpdb->term_taxonomy.taxonomy = 'category' $inExcludeQuery $catSortColumn
    			$catSortOrder";

    The post query:
    Code PHP:
    $postquery = "SELECT $wpdb->terms.term_id, $wpdb->terms.name,
    			$wpdb->terms.slug, $wpdb->term_taxonomy.count, $wpdb->posts.id,
    			$wpdb->posts.post_title, $wpdb->posts.post_name,
    			date($wpdb->posts.post_date) as 'date' FROM $wpdb->posts, $wpdb->terms,
    			$wpdb->term_taxonomy, $wpdb->term_relationships  WHERE $wpdb->posts.id =
    			$wpdb->term_relationships.object_id AND $wpdb->posts.post_status='publish'
    			AND $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id AND
    			$wpdb->term_relationships.term_taxonomy_id =
    			$wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.taxonomy =
    			'category' $isPage $postSortColumn $postSortOrder";
    Last edited by stationmaster; Dec 27, 2008 at 07:16. Reason: [Resolved]
    Daiv Mowbray twincascos Barcelona
    wordpress pluginswp-superslider.com
    portfolio portfolio.daivmowbray.com
    personal blogwww.daivmowbray.com

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a little confused on what you're trying to do, could you perhaps provide an original result, and then provide what the sorted version should look like?
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This may be what you're trying to do?

    PHP Code:
    <?php

    /**
     * Just creating some mock data
     */                  
    $foo = new stdClass();
    $foo->termId 1;
    $foo->parent 0;
                         
    $bar = new stdClass();
    $bar->termId 444;
    $bar->parent 1;
                         
    $baz = new stdClass();
    $baz->termId 999;
    $baz->parent 444;
                         
    $bat = new stdClass();
    $bat->termId 1999;
    $bat->parent 1;
                                  
    $items = array($foo$bar$baz$bat);


    foreach (
    $items as $item) {
        
    // Displays the depth of each node
        
    echo getDepth($item$items) , "\n";
    }

      
    /**          
     * Finds the relative level of depth of the provided node 
     *           
     * @param stdClass $node
     * @param array $items Original data
     * @param int $depth Current depth in search
     * @return int
     */          
    function getDepth(stdClass $node, array &$items$depth 0)
    {                           
        if (
    $node->parent == 0) {
            
    // Root node, return current depth
            
    return $depth;
        } else {
            
    // Search for matching parent node
            
    $parent array_filter(
                
    $items,        
                
    create_function(
                    
    '$item',                 
                    
    'return ($item->termId == ' $node->parent ');'
                
    )
            );                          
            
    $parent array_pop($parent);
            return 
    getDepth($parent$items, ++$depth);
        }
    }
    Output:
    Code:
    0
    1
    2
    1
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  4. #4
    SitePoint Enthusiast stationmaster's Avatar
    Join Date
    May 2003
    Location
    barcelona
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Hello bhanson,
    Yes sir, that is exactly what I was trying to do.
    Identify the level of each item based on a parent relationship.
    In my real case I needed to remove the stdClass property from the function.

    The getDepth function does a recursive dive and replies with:
    1,
    2,
    3,
    4,
    5, etc

    Thank you so much for taking the time to solve this piece of my puzzle.
    I had been trying to write something similar for days now.
    This will allow me to build a recursive data list;

    Code HTML4Strict:
    <dl>
    	<dt></dt>
    	<dd>
    	<dl>
    		<dt></dt>
    		<dd>
    			<dl>
    			<dt></dt>
    			<dd>
    				<dl>
    				<dt></dt>
    				<dd></dd>
    				</dl>
    			</dd>
    			</dl>	
    		</dd>
    	</dl>
    	</dd>
    </dl>

    As for the full code, or sample output , this is a plugin for wordpress, superslider-menu,
    which uses mootools and a multi level accordion for a fold down tree type menu system.
    Still in pre -version 1, I am working on a few bugs or errors in my code which prevented the multi level from being written properly. Your help will resolve the main problem which I've been trying to fix.
    Thanx again- Daiv
    Daiv Mowbray twincascos Barcelona
    wordpress pluginswp-superslider.com
    portfolio portfolio.daivmowbray.com
    personal blogwww.daivmowbray.com


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •