<?php $arr =Array( 0 => Array ( 0 => "string", 1 => 1, "return" => true ), "two" => Array ( 0 => "2017-10-09", 1 => 248.38, "return" => false ), 3 => -123.4, 4 => array( 0 => array( 0 => "foo", 1 => array( 0 => "bar" ) ) ) ); printArray($arr // Input array for output , "array(" // type of output "array()" or "[]" , 1 // start padding multiplied with 4. 1 = 4 spaces indenting at start. , false // is input a subarray. Always false when you call the function. Only function itself should change this ); function printArray($arr, $output, $pad, $subarray){ // If it's a subarray don't indent "array" text if($subarray){ echo str_pad("", 0, " ") . $output ."\n"; }else{ echo str_pad("", $pad*4, " ") . $output ."\n"; } $i=1; foreach($arr as $key => $item){ if(is_array($item)){ echo str_pad("", ($pad+1)*4, " "); // add "" to key if it's associative if(is_string($key)){ echo "\"" . $key. "\" => "; }else{ echo $key . " => "; } // recrusive run printArray with padding +1 (more indenting) printArray($item, $output, $pad+1, true); }else{ echo str_pad("", ($pad+1)*4, " "); // add "" to key if it's associative if(is_string($key)){ echo "\"" . $key. "\""; }else{ echo $key; } // echo item with "" if it's string, or as bool or else as numeric (float/int) if(is_string($item)){ echo " => \"". $item ."\""; }else if(is_bool($item)){ $bool = var_export($item,true); echo " => ". $bool; }else{ echo " => ". $item; } // if it's the last item, don't add comma to end of array if($i == count($arr)){ echo "\n"; }else{ echo ",\n"; } $i++; } } // add correct closing bracket if($output == "["){ echo str_pad("", $pad*4, " ") . "]"; }else{ echo str_pad("", $pad*4, " ") . ")"; } // if it's the very last item add a ; instead of , if($pad == 1){ echo ";\n"; }else{ if($i == count($arr)){ echo "\n"; }else{ echo ",\n"; } } }
You have javascript disabled. You will not be able to edit any code.