21-04783-85013 выводит как 2.83544E + 11 в моем PHP выводе массива (чтение из CSV) - PullRequest
0 голосов
/ 14 апреля 2020

РЕДАКТИРОВАТЬ: Это была моя ошибка, я смотрел на неправильную строку в CSV, этот вопрос больше не нуждается в ответе.

В соответствии с заголовком, когда я вывожу свой массив, я получаю смешные числа как 2.83544E + 11 (когда в этом случае это должно было быть 21-04783-85013.) Я попытался найти это, полагая, что это может быть что-то делать с кодировкой CSV? Имея это в виду, я попытался mb_convert_encoding($fileRow[20], 'Windows-1252', 'UTF-8'), безрезультатно.

Вот полный код

<code>function readCSV($csvFile)
{
    $output = [];
    $fileHandle = fopen($csvFile, 'r');
    $header = fgetcsv($fileHandle);
    while (!feof($fileHandle)) {
        $fileRow = fgetcsv($fileHandle, 1024);
        $orderId = $fileRow[0];
        // skip this row if it's empty (the first field contains no id)
        if (empty($orderId)) {
            continue;
        }
        /*
          $fileRow[3] is "Buyer name", the first field that's present in one type of row
          (the one containing common properties of the order). By checking if it's empty,
          we identify the contents of the row - not empty means order row with common
          properties, empty means item row with specific item properties.
         */
        if (!empty($fileRow[3])) {
            // no need to repeat the id inside the array - it's already stored in the key
            $output[$orderId] = [
                'order_number' => $fileRow[0],
                'buyer_username' => $fileRow[2],
                'buyer_name' => $fileRow[3],
                // here you can continue explicitly adding any property you need
            ];
        } else {
            // add a new item entry
            $output[$orderId]['items'][] = [
                'item_number' => $fileRow[20],
                'item_title' => $fileRow[21],
                'quantity' => $fileRow[24],
                'price' => $fileRow[25],
                // here you can continue explicitly adding any property you need
            ];
        }
    }
    fclose($fileHandle);
    return $output;
}
    // Set path to CSV file
    $csvFile = 'tmp_csv_store/my.csv';
    $csv = readCSV($csvFile);

echo "<pre>";
print_r($csv);
echo '
';

Кажется, это строка 'item_number' => $fileRow[20], мне нужно отформатировать? Предложения, если вы хотите разместить?

РЕДАКТИРОВАТЬ, чтобы добавить ..

Вывод из php result ..

[item_number] => 2.83544E+11

Вывод результата в вопросе в файле CSV (из Блокнот)

110393,21-04783-85013,wend,WENDY ,wendy@no-email.com,,10 ANY Road,Northfield,Birmingham,West Midlands,B11 1BB, //LINE CONTINUES (second item is the problem)

Ожидаемый вывод

[item_number] => 21-04783-85013
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...