ChenXiu Posted August 19, 2021 Share Posted August 19, 2021 If a json decoded array is huge (1,000,000+ lines) what is the fastest, least CPU intensive way to get it into a CSV? I only want the ["content"] portion into the .csv file: // $result = json_decode($jsonFile); print_r($result); stdClass Object ( [content] => Array ( [0] => stdClass Object ( [id] => 1234 [day] => monday [location] => USA ) [1] => stdClass Object ( [id] => 5678 [day] => thursday [location] => EU ) ) [searchCriteria] => stdClass Object ( [from] => monday [toDate] => friday ) [size] => 2 [number] => 0 ) What is the best way to loop to only get the "id" "day" and "location" from the ["content"] portion of the array in to the .csv file? $fp = fopen( 'csv_file.csv' , 'w' ); foreach( $result as $i ) { <------- this part. fputcsv( $fp , $i ); } fclose($fp); Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/313572-json-into-csv-most-efficient-for-large-files/ Share on other sites More sharing options...
Barand Posted August 19, 2021 Share Posted August 19, 2021 Decode to an array. fputcsv() doesn't play nice with objects. Then $fp = fopen( 'csv_file.csv' , 'w' ); foreach( $result['content'] as $i ) { fputcsv( $fp , $i ); } fclose($fp); Quote Link to comment https://forums.phpfreaks.com/topic/313572-json-into-csv-most-efficient-for-large-files/#findComment-1589233 Share on other sites More sharing options...
ChenXiu Posted August 19, 2021 Author Share Posted August 19, 2021 7 hours ago, Barand said: fputcsv() doesn't play nice with objects Thank you. I've done a lot of benchmarkes and indeed, the fastest speed I could reach was with exactly what you are suggested (using json_decode($result,true) and then looping through $data["content"] ). I thought there might be a way to save 500ms by "capturing" the "content" portion of the json string (using a string function while json is still a string) because string functions always are faster than looping. But maybe not.... I think monkeying with the json with string functions would open a pandoras box of errors 😃 Quote Link to comment https://forums.phpfreaks.com/topic/313572-json-into-csv-most-efficient-for-large-files/#findComment-1589241 Share on other sites More sharing options...
Barand Posted August 19, 2021 Share Posted August 19, 2021 Consider 3 things The amount of money saved by shaving a few millisecs off the execution time The cost of your time spent trying to shave those few millisecs off the execution time. Is it worth it? Quote Link to comment https://forums.phpfreaks.com/topic/313572-json-into-csv-most-efficient-for-large-files/#findComment-1589243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.