2

I have 2 arrays. 1st one is

 $people = array(
    'person1' => array('name' => 'Ted', 'Year' => '68'),
    'person2' => array('name' => 'Jane', 'Year' => '72'),
    'person3' => array('name' => 'James', 'Year' => '46'),
    'person4' => array('name' => 'Tim', 'Year' => '44'),
    'person5' => array('name' => 'Ann', 'Year' => '39'),
    'person6' => array('name' => 'Leyla', 'Year' => '45'),
    'person7' => array('name' => 'Lucy', 'Year' => '41'),
    'person8' => array('name' => 'Diana', 'Year' => '28'),
    'person9' => array('name' => 'Tony', 'Year' => '10'),
    'person10' => array('name' => 'Jane', 'Year' => '20'),
    'person11' => array('name' => 'Alex', 'Year' => '30'),
    'person12' => array('name' => 'Jane', 'Year' => '3'),
    'person13' => array('name' => 'Ted', 'Year' => '27'),
    'person14' => array('name' => 'Alex', 'Year' => '1'),
);

and the second one is

 $genTree = array(
    'families' => array(
        array(
            "mother" => 'person2',
            "father" => 'person1',
            'children' => array(
                'person3',
                'person4',
                'person5'
            )
        )
    ),
    'nextGeneration' => array(
        'families' => array(
            array(
                'mother' => 'person6',
                'father' => 'person3',
                'children' => array(
                    'person8',
                 ),
            ),
            array(
                'mother' => 'person7',
                'father' => 'person4',
                'children' => array(
                    'person9',
                    'person10'
                )
            ),
       ),
       'nextGeneration' => array(
           'families' => array(
                array(
                    'mother' => 'person8',
                    'father' => 'person11',
                    'children' => array(
                        'person12'
                    )
                ),
                array(
                    'mother' => 'person10',
                    'father' => 'person13',
                    'children' => array(
                        'person14'
                    )
                )
            ),
        ),
   ),
);

I need to get every family for each generation in new table data, so it should be arrange as a family tree

I try to do next, but not what i need

    function nameValues($array){
    global $people; echo "<table border=1>";
    foreach($array as $key=>$value)
    {

        echo "<tr>";
        if(is_array($value))
        {
            nameValues($value);
        }else{

             echo "<td>".$key." ".$people[$value]['name']."</td>";echo "</tr>";

        }
    }
    echo "</table>";


}

thanks for help and explenation

1
  • 1
    Can you provide a sample of the HTML output you would like to see? Incidentally, when you are calling nameValues recursively, it is not being put in a td so it will produce invalid HTML. Commented Oct 22, 2012 at 11:16

2 Answers 2

2

I hope this will help

echo nameValues($genTree);

function nameValues($array){
    global $people;

    $table = '<table border="1">';
    foreach($array as $key => $value){
        if($key == 'families'){
            $table .= '<tr>';
            foreach($value as $familyKey => $familyValue){
                $table .= '<td>';
                $table .= '<table border="1">';
                $table .= '<tr>';
                $table .= '<td>Mother: </td><td>'.$people[$familyValue['mother']]['name'].'</td>';
                $table .= '<td>Age: </td><td>'.$people[$familyValue['mother']]['Year'].'</td>';
                $table .= '</tr>';
                $table .= '<tr>';
                $table .= '<td>Father: </td><td>'.$people[$familyValue['father']]['name'].'</td>';
                $table .= '<td>Age: </td><td>'.$people[$familyValue['father']]['Year'].'</td>';
                if(is_array($familyValue['children'])){
                    foreach($familyValue['children'] as $childrenKey => $childrenValue){
                        $table .= '<tr>';
                        $table .= '<td>Child: </td><td>'.$people[$childrenValue]['name'].'</td>';
                        $table .= '<td>Age: </td><td>'.$people[$childrenValue]['Year'].'</td>';
                        $table .= '</tr>';
                    }
                }
                $table .= '</tr>';
                $table .= '</table>';
                $table .= '</td>';
            }
            $table .= '</tr>';
        }else{
            $table .= nameValues($value);
        }
    }
    $table .= '</table>'; 
    return $table;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry but this return only 1st family? What about other families.
Sorry, i make some change, i have edit my code,the problem was with recall again for each generation, however, copy new code and try again
1

Try like this:

function printfam($genarray,$people)
{
    if(count($genarray['families']>0))
    {
        for($temp=0;$temp<count($genarray['families']);$temp++)
        {

            $famarray=$genarray['families'][$temp];

            echo "<table>";
            echo "<tr><th>Father</th><td>".$people[$famarray['father']]['name']." (".$people[$famarray['father']]['Year'].")"."</td></tr>";
            echo "<tr><th>Mother</th><td>".$people[$famarray['mother']]['name']." (".$people[$famarray['mother']]['Year'].")"."</td></tr>";
            if(count($famarray['children'])>0)
            {

                $childrenarr=array();
                foreach($famarray['children'] as $ch)
                {
                    $childrenarr[]= $people[$ch]['name']." (".$people[$ch]['Year'].")";
                }
                echo "<tr><th>Childrens</th><td>".implode(", ",$childrenarr)."</td></tr>";
            }

            echo "</table><br />";
            if(isset($genarray['nextGeneration']))
            printfam($genarray['nextGeneration'],$people);

        }
    }
}

printfam($genTree,$people);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.