霜天部落 | 专注PHP研发,研究LAMP高性能架构部署与优化

php遍历输出数组

本文介绍一个php遍历输出数组函数,仅供学习之用。

function echo_array($array, $key) {
 static $tab_num = 0;
 if (is_array($array)) {
  print str_repeat("\t", $tab_num) . $key . " => ";
  print "array(\n";
  foreach ($array as $key => $value) {
   if (is_array($value)) {
    $tab_num++;
    echo_array($value, $key);
   } else {
    print str_repeat("\t", $tab_num +1) . $key . ' => ' . $value . "\n";
   }
  }
  if ($tab_num > 0)
   $tab_num--;
  print str_repeat("\t", $tab_num) . ")\n";
 }
}

$abc = array (
 0 => array (
  0 => 213,
  1 => array (
   0 => 4564,
   1 => 4656
  )
 ),
 'adb' => 'sdf',
 'adbwer' => array (
  'cvx' => 'sdfsdf',
  'sd' => 'sdfsd'
 )
);

echo_array($abc, 'abc');