Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Recursive Algorithm in PHP

Options
  • 17-10-2008 9:46am
    #1
    Closed Accounts Posts: 19,777 ✭✭✭✭


    I have a an array that essentially hangs off itself (each element has an ID and parent ID that references another element) and I want to be able to out put the tree that this produces in an XML format.

    Needless to say writing an algorithm to do this from scratch would be a serious pain in the arse and I've already STFW with limited success, so if anyone has a ready-made function(s) or can point me to a resource that would make my life easier, I'd greatly appreciate it.


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Post some code that generates the array and I'm sure somebody will have a go.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Pretty straightforward - one could suggest the following:
    [PHP]
    $a_MyArray = array(
    array("id" => 1, "name" => "A", "parentid" => 0),
    array("id" => 2, "name" => "B", "parentid" => 0),
    array("id" => 3, "name" => "C", "parentid" => 1),
    array("id" => 4, "name" => "D", "parentid" => 1),
    array("id" => 5, "name" => "E", "parentid" => 2),
    array("id" => 6, "name" => "F", "parentid" => 4)
    );
    [/PHP]
    And what I'm looking to get back is something like:
    <node name="A" id="1">
        <node name="C" id="3" />
        <node name="D" id="4">
            <node name="F" id="6" />
        </node>
    </node>
    <node name="B" id="2">
        <node name="E" id="5" />
    </node>
    
    Baring in mind nodes can continue to the Nth level.


  • Registered Users Posts: 2,793 ✭✭✭oeb


    What you are doing there is known as a Linked List in languages that support it. There are implimentations in php (such as http://www.phpclasses.org/browse/file/7143.html) but I have not looked into them too much. Most classes like this will have some kind of iterate() function, and the handling there should be similar to what you are looking for.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I remember writing a function which converted a flat array into a tree-style multidimensional array. I'll see if I can dig it out and see if it's any use to you.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    This might need a bit of tweaking but give it a try:
    <?php
    function subnode($parentid, $array)
    {
    	$i = $parentid;
    	while($i < sizeof($array))
    	{
    		if($array[$i].parentid == $parentid)
    		{
    			echo "<node name=\"".$array[$i].name."\" id=\"".$array[$i].id."\" >";
    			subnode($array[$i].id, $i, $array);
    			echo "</node>";
    		}
    		$i++;		
    	}
    }
    
    $j = 0;
    while($j < sizeof($a_MyArray))
    {
           if($a_MyArray[$j].parentid == 0)
           {
                   subnode($a_MyArray[$j].id, $a_MyArray);
           }
           $j++;
    }
    ?>
    

    Unfortunately, I don't have an apache server to hand to test this so as I said you might need to tweak it but the logic should hold. If not at least it's a pointer in the right(ish) direction :)

    Regards,
    RD


  • Advertisement
Advertisement