note 43329 deleted from function.print-r by aidan

From: Date: Mon, 11 Oct 2004 13:07:17 +0000
Subject: note 43329 deleted from function.print-r by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-78342@lists.php.net to get a copy of this message
Note Submitter: matt at crx4u dot com ---- Here's a function I use to quickly capture an array for testing in other code. It converts an array into code. <?php /** * Convert an array to code. * Recursively convert any sub arrays. */ function array_to_code($array, $nested_level=0) { $code_string = ""; // Convert each element into code. if (is_array($array)) { // Create the indent. $indent = str_repeat("\t", $nested_level); $code_string .= "array(\n"; foreach ($array as $key => $value) { // Convert this sub array to code. $element_code = array_to_code($value, $nested_level+1); // Add the new code to the string. $code_string .= $indent."\t\"".$key."\" => ".$element_code.",\n"; } $code_string .= $indent.")"; } else { // Not an array: just return with quotes. $code_string = "\"".addslashes($array)."\""; } return $code_string; } ?> Example use: <?php $test_array = array("name" => "Matt", "phone" => "1234567890", "days" => array("1"=>"Monday","2"=>"Tuesday")); print array_to_code($test_array); ?> Output: array( "name" => "Matt", "phone" => "1234567890", "days" => array( "1" => "Monday", "2" => "Tuesday", ), )

« previous php.notes (#78342) next »