Version: 1.0

Type: Function

Category: Algorithms

License: GNU General Public License

Description: have you ever wanted to quickly see the contents of an array? This function dumps an array into a string recursively using key:value pairs. All values are tested to see if they are an array, and if so, are split into key:value pairs recursively until everything is a string. The key:value seperators, and key:key seperators are configurable through the function call (you don't have to change any variables).



<?php
// sytax: string dump_array(mixed $value [, string $value_boundary [, string $key_boundary [, int $r_flag]]])
// dumps the array into "key : value" pairs recursively:
// argument 1 is required, and can be any variable type... but if its an array, it will be dumped recursively.
// argument 2 is optional and is a string. This string is the boundary between values. the default is " : "
// argument 3 is optional and is a string. This string is the boundary between the keys. The default is "<br>";
// argument 4 is optional and is a number. 1 means that the function call is a recursive one, and not the original.
// argument 4 is for internal function use only and should not be set in the original function call from the script.
function dump_array ($x) {
  if (gettype($x) == "array") { // if value is an array...
    // then the value is split into key:value pairs, which are each sent back through the function
	// to make sure they are not arrays themseles... and if they are, they are split again, and again, and again, etc...
    $vb = ' : ';  // default boundary that seperates values from each other and their key.
	$kb = '<br>'; // default boundary that seperates keys.
    if (func_num_args() > 1) { // if arg 2 is set...
	  $vb = func_get_arg(1);  // the value replaces the default value boundary.
	  if (func_num_args() > 2) { // if arg 3 is set...
	    $kb = func_get_arg(2); // the value replaces the default key boundary.
	  }
	}
	/*if (func_num_args() > 3 && func_get_arg(3) == 1) { // if the function call is a recursive one...
	  $kb = $vb; // the key boundary is removed, since the key is really just a value for another key.
	}*/
    while ($a = each($x)) { // walks through the key:value pairs for each key in the current array.
	  // a string containing all the key:value pairs is generated:
	  // - both the key and the value are sent back through the function to ensure they are not arrays.
	  if (func_num_args() > 3 && func_get_arg(3) == 1) {
	    @$table .= '(' . dump_array($a[0], $vb, $kb, 1) . $vb . dump_array($a[1], $vb, $kb, 1) . ')';
	  } else {
		@$table .= dump_array($a[0], $vb, $kb, 1) . $vb . dump_array($a[1], $vb, $kb, 1) . $kb;
	  }
	}
	return $table;
  } else { // if value is not an array, the value is returned untouched.
    return $x;
  }
}
?>