Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Recursive Algorithm in PHP

  • 17-10-2008 09: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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 68,173 ✭✭✭✭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