chiprivers Posted May 9, 2007 Share Posted May 9, 2007 Can anyone help me with a function to display the keys/values of a multidimensional array in a more simpler to read display than that of the standard print_r()? Quote Link to comment https://forums.phpfreaks.com/topic/50722-display-values-of-multidimensional-array/ Share on other sites More sharing options...
utexas_pjm Posted May 9, 2007 Share Posted May 9, 2007 <?php echo '<pre>'.print_r($array, true).'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/50722-display-values-of-multidimensional-array/#findComment-249358 Share on other sites More sharing options...
Daniel0 Posted May 9, 2007 Share Posted May 9, 2007 I don't know what you consider easier to read, but here is what you could do: <?php function display_array($array, $level=0) { foreach($array as $key => $value) { $output .= str_repeat("\t",$level); if(!is_array($value)) { $output .= "{$key} = {$value}\n"; } else { $output .= "{$key} = Array:\n"; $output .= display_array($value,$level+1); } } return $output; } $array = array( 'hello' => 'world', 'test' => array( 'sub-thing' => 'hi', 'new_array' => array( 'test1', 'test2', 'test3', array(1,2,3,4), ), ), 'bla' => 'php', ); echo display_array($array); ?> Output is: hello = world test = Array: sub-thing = hi new_array = Array: 0 = test1 1 = test2 2 = test3 3 = Array: 0 = 1 1 = 2 2 = 3 3 = 4 bla = php Quote Link to comment https://forums.phpfreaks.com/topic/50722-display-values-of-multidimensional-array/#findComment-249363 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.