Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with loop

    Hi,
    I have this array:
    PHP Code:
    array('a'=>array('a1','a2'), 'a1'=>array('a1.1','a1.2','a1.3'), 'a1.2'=>array('a1.2.1')); 
    and I wish to do a replacement of values so it becomes this:

    PHP Code:
    array('a'=>array(array('a1.1',array('a1.2.1'),'a1.3'),'a2')); 
    Have been trying and trying but I still can't figure out....
    Any help is greatly appreciated.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    PHP Code:
    function recRepl($array)
    {
        
    _recRepl($array$array);
        return 
    $array;
    }
    function 
    _recRepl(&$array, &$values)
    {
        foreach(
    $array as &$value)
        {
            if(!
    is_array($value) && isset($values[$value]))
            {
                
    $vTemp $value;
                
    $value $values[$value];
                unset(
    $values[$vTemp]);
            }
            if(
    is_array($value))
            {
                
    _recRepl($value$values);
            }
        }
    }

    $x = array('a'=>array('a1','a2'), 'a1'=>array('a1.1','a1.2','a1.3'), 'a1.2'=>array('a1.2.1'));  
    print_r(recRepl($x)); 
    dumpfi
    Last edited by dumpfi; 12-23-2006 at 06:47 PM.
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

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