I want help for recursive in php which like structure is attachment file..

[1] Sire (Parent)

 > Sub1
 > Sub2

[2] Dam (Parent)

 > Sub1
 > Sub2

Do you have some example of code so its better for me.
Thank You.

Attachments aCdb6.png 21.77 KB
2 Years
Discussion Span
Last Post by Taywin

Huh? What does your recursive function do? What are inputs and what output you want? Or you simply want a search for something? The word recursive is very broad.


@Taywin:Given In Details.

[1]I have One dropdown box there have number of Goat Names.
[2]When select it then it geneted his Sir and Dam Names.
[3] If Sir and Dame have his N-childs then its also Display Names.

Names records come from database simple.


OK, so the input is a name used in query to get from database. You must always define base cases for a recursive function, or it will run forever! A very simple idea for the function (Depth-First-Search) is as follows...

function a_recursive_function(name)
  data = get_data_from_database(name)
  if data not exist  // base case
  end if
  display data(SireName)
  display data(DamName)
end function

Edited by Taywin

Votes + Comments
@Thank You.Now its work

We always try to prevent ourselves from sinking into recursive function in PHP. However, if that is what you need, please consider the simple function I wrote to demonstrate how recursion works.

Recursion in PHP is a function or method that calls itself within itself.

Huh!???? I know my definition is a little blurry. For a moment allow me to provide a generic example without logic.

function genericFunction()

    return genericFunction();


We can make our simple non-logical function above as template.

Let say we have a paren array as shown below,

$parent = array(
                    'a'=>(array('a_one' => 'a_one value','a_two'=>'a_two value', 'a_three' => 'a_three value')),
                    'b'=>(array('b_one' => 'b_one value','b_two'=>'b_two value', 'b_three' => 'b_three value'))

and we want a function that will give us the $parent['a'] and $parent['b'] recursively.

Array ( [a_one] => a_one value [a_two] => a_two value [a_three] => a_three value ) 
Array ( [b_one] => b_one value [b_two] => b_two value [b_three] => b_three value )

we can simply write a function that can do or provide us an output like the above.

the function::

function find_child_in_parent($child,$parent = array())


        foreach($parent as $p_key => $p_value){

            if($p_key == $child){

                return ( $p_value );
            } // endif

         } //endforeach


            //recursion here
            $recursion_res = find_child_in_parent($child,$p_value);

            return($recursion_res ? $recursion_res : FALSE);


            return FALSE;



we call the function like this

 $parent = array(
                    'a'=>(array('a_one' => 'a_one value','a_two'=>'a_two value', 'a_three' => 'a_three value')),
                    'b'=>(array('b_one' => 'b_one value','b_two'=>'b_two value', 'b_three' => 'b_three value'))

     echo '<br/>';


the result will be the same as I expected it shown in the earlier paragraph.

Good luck to you. please feel free to experiment. Don't be afraid in making mistakes. :)...

Edited by lorenzoDAlipio: corrected codes


@Taywin: as you guide me here i have worked but its going infinite.

function generate_tree($sire)
 global $wpdb;

    $sql = $wpdb->prepare("SELECT * FROM mm_goats WHERE goat_name = %s", $sire);
    $rowsire = $wpdb->get_row($sql, ARRAY_A);

    echo "done";

    echo $rowsire['sire'];            //this porsion come infinite as output

    echo $rowsire['dam'];              //this porsion not come in output


You need to return (exit the function) right after line 10 inside the if statement. ;)

Edited by Taywin

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.