Home >Backend Development >PHP Tutorial >PHP traverse two-dimensional array and multi-dimensional array code_PHP tutorial
PHP traverses two-dimensional arrays and multi-dimensional array codes Regarding PHP arrays, let's talk about two-dimensional arrays and multi-dimensional traversal codes. Here are three examples to test the array traversal PHP code function.
php tutorial traversing two-dimensional array and multi-dimensional array code
Regarding PHP arrays, let’s talk about two-dimensional arrays and multi-dimensional traversal codes. Here are three examples to test the array traversal PHP code functions.
*/
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)),2,3,4,5);
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, traversing a two-dimensional array
$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';
}
?>