loading...

re: CSV Challenge VIEW POST

FULL DISCUSSION
 

Here is my take in this challenge in PHP.

<?php
    // Getting Data.json contents
    $inMemoryData = file_get_contents("https://gist.githubusercontent.com/jorinvo/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e3619/data.json");
    $convertToArray = json_decode($inMemoryData);
    echo "Whole Data Count ".count($convertToArray).PHP_EOL;
    // Filtering th data using array_filter method
    // $filteredData = array_filter($convertToArray, function ($arr){
    //     return !empty($arr->creditcard) ? 1 : 0;
    // });
    // Create a file to save data
    $fileHandle = fopen(date("Ymd").".csv", "w");
    // Save Array of to CSV
    fputcsv($fileHandle, ["Name", "Credit Card"]);
    // Filtering data
    foreach ($convertToArray as $data) {
        if (!empty($data->creditcard)) {
            fputcsv($fileHandle, [
                $data->name,
                $data->creditcard
            ]);
        }
    }
    echo "Completed. ".PHP_EOL;
?>
Code of Conduct Report abuse