Php array recursive output

Source: Internet
Author: User
Php array recursive output this post is finally edited by ecshop528 from 2013-01-211:54:22 $ ar & nbsp ;=& nbsp; array ('0' = & gt; array ('name' = & gt; 'name1', 'age' = & gt; 'age1 '), '1' = & gt; arra php array recursive output
This post was last edited by ecshop528 at 12:54:22 $ ar = array ('0' => array ('name' => 'name1', 'age' => 'age1 '), '1' => array ('name' => 'name2', 'age' => 'age2 '), '2' => array ('name' => 'name3', 'age' => 'age3 '));

To use recursion, enter the following string



Name3-age3

Name2-age2

Name1-age1

$ar = array('0'=>array('name'=>'name1','age'=>'age1'),'1'=>array('name'=>'name2','age'=>'age2'),'2'=>array('name'=>'name3','age'=>'age3'));
deep_child($ar);
function deep_child($ar = array()){
static $str = '';
static $i=0;

if($ar[$i]){
echo '';
$s =  $ar[$i]['name']."-".$ar[$i]['age'];
$i++;
deep_child($ar);
echo $s;
echo '';

}
}


In this way, you can write a good image and do not know whether there are any other methods ..
------ Solution --------------------
Static variables are not available at will.
$ar[0] = array('0'=>array('name'=>'name1','age'=>'age1'),'1'=>array('name'=>'name2','age'=>'age2'),'2'=>array('name'=>'name3','age'=>'age3'));
$ar[1] = array('0'=>array('name'=>'name1','age'=>'age1'),'1'=>array('name'=>'name2','age'=>'age2'),'2'=>array('name'=>'name3','age'=>'age3'));
for($i=0;$i<2;$i++){
    echo deep_child($ar[$i]);
     
}
function deep_child($ar = array(), $i=0){
    $str = '';
//    static $i=0;
     
    if(isset($ar[$i])){
        $str.= ''.PHP_EOL;
        $str.=  $ar[$i]['name']."-".$ar[$i]['age'].PHP_EOL;
        $i++;
        $str.=deep_child($ar, $i);
         
        $str.= ''.PHP_EOL;
     
    }
    return $str;
}

Name1-age1

Name2-age2

Name3-age3




Name1-age1

Name2-age2

Name3-age3



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.