如何在CSV文件中正确编写数组?

时间:2016-07-11 03:16:57

标签: php csv

我正在尝试将数组写入excel文件。我用循环来生成数组。当我打印数组时,它给了我98条记录但是在excel文件中它写了90条结果。如果array有150个结果,那么它也会写出90个结果。这是我的代码。

<?php
    error_reporting(0);
    ob_start();
    ini_set('max_execution_time', 0); //300 seconds = 5 minutes
    ini_set('memory_limit', -1);
    include_once('classes/html_dom.php');

    if ($_GET['q']) {
        $mobile = $_GET['q'];
    }
    $filename = ucfirst($mobile);

    for($i=1; $i<=4;$i++) {

        //$target_url = 'https://www.awok.com/mobile-phones/ds-582/?brand='.$mobile.'&set-filter=Y&PAGEN_1='.$i.'';

        $target_url = 'Source-Files/Awok-'.$filename.'-Mobile-'.$i.'.html';

        $html = new simple_html_dom();

        $html->load_file($target_url); 

        $SiteUrl = 'https://www.awok.com';

        $products       = array();

        foreach($html->find('div.subcat_cat_item_text a ') as $link_1) {

                foreach($link_1->find('p.subcat_cat_item_title') as $link_2) {
                    foreach($link_1->find('span.subcat_cat_item_newprice') as $link_3) {
                        array_push($products, array("Mobile Name" => $link_2->plaintext , "Link" => $SiteUrl.$link_1->href , "Price" => $link_3->plaintext));

                    }

                }

            } 
        /*echo '<pre>'; print_r($products); echo '</pre>';*/

            $output = fopen("php://output",'w') or die("Can't open php://output");
            header("Content-Type:application/csv"); 
            header("Content-Disposition:attachment;filename=Awok ".$filename." Mobile List.csv"); 
            header('Cache-Control: max-age=0');

                foreach($products as $product) {
                    fputcsv($output, $product);
                }
                fclose($output) or die("Can't close php://output");     
        ob_flush();
    }
?>

0 个答案:

没有答案