Advertisement
raden_rangga

Konversi Json 2 CSV

Dec 20th, 2022
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | Source Code | 0 0
  1. <?php
  2. // Read the JSON file
  3. $json = file_get_contents('text.json');
  4.  
  5. // Decode the JSON file
  6. $json_data = json_decode($json, true);
  7.  
  8. // Display data
  9. // print_r($json_data);
  10. echo "firstName;lastName;gender;age;number\r\n";
  11. foreach ($json_data as $key) {
  12.     echo $key["firstName"].";".$key["lastName"].";".$key["gender"].";".$key["age"].";".$key["number"]."\r\n";
  13. }
  14.  
  15. // Open a file in write mode ('w')
  16. $fp = fopen('text.csv', 'w');
  17.  
  18. // Loop through file pointer and a line
  19. foreach ($json_data as $key) {
  20.         fputcsv($fp, $key, ";");
  21. }
  22.  
  23. fclose($fp);
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement