Posts

Showing posts with the label php

Json_encode function to print php json indent

Image
When encoding an associative array to json using the json_encode function in php, it is output as a single line without indentation. <?php $arr[ 'user' ] = "kim" ; $arr[ 'age' ] = "15" ; $arr[ 'phone_number' ] = "1111-1111" ; echo json_encode($arr); ?> Results If the JSON is long, you can use a site that sorts JSON because it is very hard to see. https://jsonformatter.curiousconcept.com/   JSON Formatter & Validator Format and validate JSON data so that it can easily be read by human beings. jsonformatter.curiousconcept.com This json encoding process is provided in php's json_encode by default. Just give JSON_PRETTY_PRINT as the second argument of json_encode. This argument has been supported since php 5.4. <?php $arr[ 'user' ] = "kim" ; $arr[ 'age' ] = "15" ; $arr[ 'phone_number...

Reading by large file line in php

When a large file in php is read as file_get_contents, it is loaded into memory at once, and a situation in which php script cannot be executed due to insufficient memory occurs. At this time, if you read line by line, you can avoid running out of memory. Below is an example code $handle = fopen( "inputfile.txt" , "r" ); if ($handle) { while (($line = fgets($handle)) !== false ) { // process the line read. } fclose($handle); } else { // error opening the file. } Ok sympathy share Writing elements Subscribe