3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; } } }
Output for 7.0.0 - 7.0.24, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
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" ), ) ) );
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
177.32 ms | 407 KiB | 5 Q