php遍历二维数组与多维数组代码

关于php的数组,我们来讲一下,二维数组与多维的遍历代码,下面举了三个实例来测试数组的遍历php代码函数了,代码如下:

  1. function arr_foreach ($arr) {
  2. if (!is_array ($arr)) {
  3. return false;
  4. }
  5. foreach ($arr as $key => $val ) {
  6. if (is_array ($val)) {
  7. arr_foreach ($val);
  8. } else {
  9. echo $val.'<br/>';
  10. }
  11. }
  12. }
  13. $arr1 = array (1=>array(11,12,13,14=>array(141,142)),2,3,4,5);
  14. arr_foreach ($arr1);

php遍历多维数组,代码如下:

  1. $array = array('a'=>"\'as","b"=>array('c'=>"\'cc","n"=>array('1'=>"\'sdf")),'f'=>array('c'=>"'sdf","g"=>array("c")));
  2. function handleeach(&$array,$functionname)
  3. {
  4. foreach($array as $k=>$v)
  5. {
  6. if(is_array($v))
  7. {
  8. handleeach(&$array[$k],$functionname);
  9. }
  10. else
  11. $array[$k] = $functionname($v);
  12. }
  13. }
  14. handleeach($array,'stripslashes');
  15. print_r($array);

实例三,遍历二维数组,代码如下:

  1. $employee[]=array("jas,join","www.phpfensi.com",20);
  2. $employee[]=array("june,join","programmer",20);
  3. $employee[]=array("aili,join","programmer",20);
  4. $employee[]=array("doe,jane","programmer",20);
  5. $newname = array();
  6. foreach ($employee as $record){
  7. $newname[] = isset($record[0]) ? $record[0] : 'no name';
  8. }