1

I'm trying to get data from .CSV , sort it in array (using PHP), select specific values in it, create an new array fron this selection and encode it to a json object usable with ajax.

With this code, I'm able to get almost what I want.

$CSV_url = '../fichier-csv/commande.csv';                 // CSV file 
$csv["data"] = array_map('str_getcsv', file($CSV_url));   // to PHP array
array_shift($csv["data"]);                                // remove first line (headers)
$jsonData = json_encode($csv);                            // to Json

The CSV file contains about 16 categories of data per line, but i only need 4 of them in my final json.

I'm stuck with this result . If I print before json_encode ($csv), I get a nice array, but I've trouble to select the wanted data and making new array with it (which is the way I think this should be done, but i'm a beginner, maybe there is a more simple way to do it). Outputed Json should also be sorted in ascending order of the [0] value of each arrays. Here is the beginnig of the arrays I get (378 of them to proccess):

Array (
    [data] => Array
        (
            [0] => Array
                (
                    [0] => 100120148
                    [1] => 1.0000
                    [2] => 7.4200
                    [3] => 2.4700
                    [4] => 4.9100
                    [5] => 0.0000
                    [6] => 14.8000
                    [7] => 0.3000
                    [8] => marie
                    [9] => jardin
                    [10] => 40 rue mazé
                    [11] => 87400
                    [12] => 066825197
                    [13] => [email protected]
                    [14] => 1
                    [15] => [email protected]
                )

            [1] => Array
                (
                    [0] => 100120162
                    [1] => 1.0000
                    [2] => 45.7500
                    [3] => 10.8000
                    [4] => 8.2500
                    [5] => 0.0000
                    [6] => 64.8000
                    [7] => 2.0000
                    [8] => __
                    [9] => Lucien fizon
                    [10] => 56 impasse pazarre
                    [11] => 25000
                    [12] => 0649851473
                    [13] => [email protected]
                    [14] => 4
                    [15] => [email protected]
                )

How do I get values 0, 8, 9 and 12 of each sub-array and create a new array with it, so I can encode this desired array to Json ?

Thanks.

2 Answers 2

1

It may be better to process it as you read the file, using fopen() and fgetcsv() you can extract just the items you want to the result rather than storing it all.

$CSV_url = '../fichier-csv/commande.csv';
$fh = fopen($CSV_url, "r");
$csv = [ "data" => [] ];
fgets($fh);             // Ignore header
while ( $row = fgetcsv($fh) )   {
    $csv["data"][] = [$row[0], $row[8], $row[9], $row[12]];
}
fclose($fh);
$jsonData = json_encode($csv);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot @Nigel Ren! I spend so much time with my nose on the solution... If it is not too much to ask, May you have an idea about how to sort the obtained PHP array by the value of the [0] row; in ascending order (from lower value to higher value )?
You may be able to get away using sort( $csv['data'] );
I thought this kind of sorting would require more parameters, "sort" did the job. I was looking into array_multisort as i thought it necessary to specify the row used to sort the array. Then again, thank you very much @Nigel ren!
0

You can use array_map again to select only the desired columns in each row:

$csv["data"] = array_map(fn($row) => [$row[0], $row[8], $row[9], $row[12]], $csv["data"]);

If you're not on PHP 7.4 yet you'll need to use the traditional syntax instead of an arrow function:

$csv["data"] = array_map(function(row) {
    return [$row[0], $row[8], $row[9], $row[12]];
}, $csv["data"];

1 Comment

Nice to have another approach to test, thanks!. Il worked well with a few edits (in PHP 7.3.12): > $csv["data"] = array_map(function($row) { > return [$row[0], $row[8], $row[9], $row[12]]; > }, $csv["data"]); > sort ($csv["data"]); > $jsonData = json_encode($csv);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.