使用PHP进行大型CSV更改


Make large CSV changes with PHP

我将首先解释我的问题…我有一个CSV文件,每天导入到我的服务器。

然后我想将CSV文件的内容导入到Magento平台作为产品,但是我的CSV文件没有正确设置Magento导入。

这是CSV文件的前两行

Feed_Id,Vehicle_ID,FullRegistration,Colour,FuelType,Year,Mileage,Bodytype,Doors,Make,Model,Variant,EngineSize,Price,Transmission,PictureRefs,ServiceHistory,PreviousOwners,Category,FourWheelDrive,Options,Comments,New,Used,Site,Origin,v5,Condition,ExDemo,FranchiseApproved,TradePrice,TradePriceExtra,ServiceHistoryText,Cap_Id,AttentionGrabber
119345,679049,VO10OHS,BLACK,Diesel,2010,77000,Estate,5,AUDI,Q7,3.0 TDI QUATTRO S LINE 5d AUTO 240 BHP 0% FINANCE ,2967,24495,Automatic,"http://static.click247.co.uk/vehicles/679/679049/large1/3672310.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672315.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672316.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672317.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672313.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672312.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672314.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582603.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582604.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672321.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3672322.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582591.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582592.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582673.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582605.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582593.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582594.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582597.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582606.jpg,http://static.click247.co.uk/vehicles/679/679049/large1/3582598.jpg",Y,0,CARS,Y,"Adjustable Steering Column/Wheel - Rake/, AUDI MUSIC INTERFACE, AUDI PARKING SYSTEM, BLACK STYLING PACK, BOSE SURROUND SOUND, ELECTRIC TAILGATE, HDD-BASED SATELLITE NAVIGATION SYSTEM, MOBILE PHONE PREPARATION, PEARL EFFECT PAINT, PRIVACY GLASS, SMART BEAM, STAINLESS STEEL RUNNING BOARDS, VUSIBILITY PACKAGE, Air Bag Driver, Air Bag Passenger, Air Bag Side - Driver/Passenger, Alarm , Anti-Lock Brakes, Armrest - Front/Rear, Ashtray/Lighter, Bumpers, Central Door Locking - Remote , Centre Console, Centre Rear Seat Belt, Child Locks - Electric, Cigar Lighter, Climate Control, Computer - Driver Information System, Cruise Control, Cup Holder, Electric Windows - Front/Rear, Electronic Brake Force Distribution, Electronic Stability Programme, Front Fog Lights, Head Air Bags - Front/Rear, Head Restraints - Front/Rear, Heated Rear Screen, Immobiliser, In Car Entertainment - Radio/CD, Mirrors External - Electric Heated/Foldi, Mirrors Internal - Auto Dimming Rear Vie, Parking Aid - Rear, Power Sock",Comprehensive RAC  parts and labour warranty available on selected vehicles-FINANCE AVAILABLE on selected vehicles-Call 01204 393181 for more details. Please click on the Dealers Website link to view more details and larger photos. REF:KEZW,N,Y,C,UK,Y,,,,0,,Full Dealer,,MEGA SPEC OVER £8000 EXTRAS

最好复制并传递CSV数据并将其保存为CSV,以便进一步了解问题。

如果你看第16列,你会注意到有一堆URL图像用逗号分隔,但在一些引号内。

我想将第一个URL图像放在引号内,并将其放入该行内的额外列中。

除了第一行,我想对CSV中的每一行都这样做。

任何想法,如果这是可能的PHP,如果是这样,你可以给我一个例子,这可能是如何做到的?

多Appricated

为$file_locatoin赋值并尝试以下操作

<?php
        $final_csv = array();
        $file_location = 'your_csv_file.csv';//assign your csv path here
        $file = fopen($file_location, "r");
        //get the file header/column name
        $file_heading = fgetcsv($file);
        //set a new column nanme as new_column
        array_push($file_heading,'new_column');
        //assign header for final csv
        array_push($final_csv,$file_heading);
        while (!feof($file)) {
            $row_data = fgetcsv($file);
            //get 16 number column
            $column = $row_data[15];
            $image_url = explode(',',$column);
            $first_img = $image_url[0];
            array_push($row_data,$first_img);
            array_push($final_csv,$row_data);
        }
        //var_dump($final_csv);
        fclose($file);

        convert_to_csv($final_csv, 'new.csv', ',');
        function convert_to_csv($input_array, $output_file_name, $delimiter)
    {
        /** open raw memory as file, no need for temp files, be careful not to run out of memory thought */
        $f = fopen('php://memory', 'w');
        /** loop through array  */
        foreach ($input_array as $line) {
            /** default php csv handler **/
            fputcsv($f, $line, $delimiter);
        }
        /** rewrind the "file" with the csv lines **/
        fseek($f, 0);
        /** modify header to be downloadable csv file **/
        header('Content-Type: application/csv');
        header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
        /** Send file to browser for download */
        fpassthru($f);
    }
?>