Php traverses two-dimensional array and multi-dimensional array code

Source: Internet
Author: User

Php traverses two-dimensional arrays and multi-dimensional array Code. For the php array, let's look at the two-dimensional array and multi-dimensional traversal code, the following three examples are provided to test the php code function for Array traversal.

Php Tutorial: traverse two-dimensional array and multi-dimensional array code
For php arrays, let's take a look at the two-dimensional array and multi-dimensional traversal code. The following three examples are provided to test the php code function for Array traversal.
*/

Function arr_foreach ($ arr ){
If (! Is_array ($ arr )){
Return false;
}
Foreach ($ arr as $ key => $ val ){
If (is_array ($ val )){
Arr_foreach ($ val );
} Else {
Echo $ val .'
';
}
}
}
 
$ Arr1 = array (1 => array (11,12, 13,14 => array (141,142 );
Arr_foreach ($ arr1 );

// Php traverses multi-dimensional arrays

$ Array = array ('A' => "'As", "B" => array ('c' => "'CC ", "n" => array ('1' => "'sdf"), 'F' => array ('c' => "'sdf ", "g" => array ("c ")));
Function handleeach (& $ array, $ functionname)
{
Foreach ($ array as $ k => $ v)
{
If (is_array ($ v ))
{
Handleeach (& $ array [$ k], $ functionname );
}
Else
$ Array [$ k] = $ functionname ($ v );
}
}
Handleeach ($ array, 'strips tutorial lashes ');
Print_r ($ array );

// Example 3: traverse two-dimensional arrays

$ Employee [] = array ("jas, join ",");
$ Employee [] = array ("June, join", "programmer", 20 );
$ Employee [] = array ("aili, join", "programmer", 20 );
$ Employee [] = array ("doe, jane", "programmer", 20 );

$ Newname = array ();

Foreach ($ employee as $ record ){

$ Newname [] = isset ($ record [0])? $ Record [0]: 'No name ';

}

?>


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.