Jump to content

display values of multidimensional array


chiprivers

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.