www.webdeveloper.com
Results 1 to 5 of 5

Thread: reversing object list

  1. #1
    Join Date
    Jan 2004
    Posts
    484

    reversing object list

    I have been at this for several hours but to no avail...

    I'd like to take this
    PHP Code:
    $lot = new stdClass;
    $lot->= new stdClass;
    $lot->a->= new stdClass;
    $lot->a->b->= new stdClass;
    $lot->a->b->c->= new stdClass
    and reverse the order.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    What do you mean by "reverse the order" -- what order?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2004
    Posts
    484
    PHP Code:
    $lot->a->b->c->d  => d->c->b->a->$lot 

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Other than reversing the order of variable names when you initially define it, I don't see any simple solution -- and at least in this trivial example don't see why you would want to. I guess that's part of the problem for me here: I don't understand "why", and thus I'm not sure what would be the best solution (if there is one).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2004
    Posts
    484
    I am apply for a position this is one of the bonus questions I figure it would help my chances at the position. they want it solved using a recursive function, reverse a link list. Anyway if you can help that would be awesome

    This is what I have:

    I maybe way off so be warned
    PHP Code:
    $lot = new stdClass;
    $lot->= new stdClass;
    $lot->a->= new stdClass;
    $lot->a->b->= new stdClass;
    $lot->a->b->c->= new stdClass;
    /*var_dump($lot);
    var_dump($list11);
      
    print print_r($list11->a);*/
      
    function reverseList($orgL$step=0$refArr=array(), $tranL=NULL$pointer=NULL$newL=NULL$objArr=array())
    {

        
        if(
    $step == 0)
        {
            
    $step 1;
            
    $tranL $orgL;
              
    $c key((array)$orgL);
            
    array_unshift($refArr,$c);
            
    //echo $array[0]['a']['b'];
        
    }
            
        if(
    $step == 1)
        {

            
            try{
                 if(empty(
    $tranL->$refArr[0]))
                    {
                    throw new 
    Exception("Value must not be empty");
                    }            
                
                
    $c key((array)$tranL->$refArr[0]);
                
    $tranL = (object)$tranL->$refArr[0];
                
    array_unshift($refArr,$c);    


            }catch(
    Exception $e){
                
    $step 2;
                
    $pointer 0;
                
    //$refArr = array_reverse($refArr);
            
            
    }
        }
            
        if(
    $step == 2)
        {
            
            print 
    "<pre>";
            
    print_r($tranL);
            print 
    "</pre>";
            
                    
                
    //we have the referances but not the empty objects
                
    $c count($refArr);
                         
                if(
    $c>$pointer)
                {    

                                
                        
    $tranL $orgL->$refArr[$pointer];
                        
    $pointer++;    
            

                }
                
        }
        
    reverseList($orgL$step$refArr$tranL$pointer$newL$objArr);
    }

    reverseList($lot); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles