Foreach通过一个范围来操作URL以从实时CSV文件中获取数据

时间:2014-03-26 20:54:26

标签: php csv methods

我正在玩一些课程,以从纳斯达克检索公司信息。公司信息基于给定的字母提取。一切正常,我可以通过提供字母和数字来获取业务,但如果我尝试设置范围并使用foreach语句查看字母,则唯一检索的是来自第一个的信息信。

class Finance {


public static $companyList = array();

public static function getCompaniesByLetter($letter = null,$number = null) {
    if ($letter && $number) {
        $letter = trim(strtoupper($letter));
        $companyList = 'http://www.nasdaq.com/screening/companies-by-name.aspx?letter='.$letter.'&render=download';
        if (@file_get_contents($companyList)) {
            self::$companyList = null;
            $csv = trim(str_replace('"Symbol","Name","LastSale","MarketCap","ADR TSO","IPOyear","Sector","industry","Summary Quote",', '', file_get_contents($companyList)));
            time_nanosleep(0, 300000000);
            $csv = file_put_contents('tmpcompanylist.csv', $csv);
            $file = fopen('tmpcompanylist.csv', 'r');
            $i = 0;
            while (!feof($file) && $i < $number) {
                $line = fgets($file);
                $line = str_replace('"', '', $line);
                $companyInfo = explode(',', $line);
                $removeThisNumber = floatval($companyInfo[2]);
                if (!$removeThisNumber) {
                    $sector = 7;
                    $industry = 8;
                } else {
                    $sector = 6;
                    $industry = 7;
                }
                $clearCompanyName = str_replace($removeThisNumber, '', $companyInfo[2]);
                $companyName = $companyInfo[1].$clearCompanyName;
                self::$companyList[] = array(
                    'symbol' => $companyInfo[0],
                    'name' => $companyName,
                    'sector' => $companyInfo[$sector],
                    'industry' => $companyInfo[$industry]
                    );
                $i++;
            }

            fclose($file);
            return true;
        }

        return false;
    }

    return false;
}


//This method will insert the companyinformation in database
public function insertCompaniesByLetter($range = array(),$number = null) {
    if (is_array($range) && $number && count($range == 2)) {
        $range = range($range[0], $range[1]);
        foreach ($range as $letter) {
            if (self::getCompaniesByLetter($letter,$number)) {

                foreach (self::$companyList as $company => $data) {
                    echo ($data['symbol']).'<br />';
                    echo ($data['name']).'<br />';
                    echo ($data['sector']).'<br />';
                }

                return true;
            }
        }
    }

    return false;
}

}

所以我尝试取公司的方式是这样的:

Finance::insertCompaniesByLetter(array('A','D'),'10');

但由于某种原因,结果是不受欢迎的:

Array
(
[0] => Array
    (
        [symbol] => PIH
        [name] => 1347 Property Insurance Holdings Inc.
        [sector] => n/a
        [industry] => n/a
    )

[1] => Array
    (
        [symbol] => FLWS
        [name] => 1-800 FLOWERS.COM Inc.
        [sector] => Consumer Services
        [industry] => Other Specialty Stores
    )

[2] => Array
    (
        [symbol] => FCTY
        [name] => 1st Century Bancshares Inc
        [sector] => Finance
        [industry] => Major Banks
    )

[3] => Array
    (
        [symbol] => FCCY
        [name] => 1st Constitution Bancorp (NJ)
        [sector] => Finance
        [industry] => Savings Institutions
    )

[4] => Array
    (
        [symbol] => SRCE
        [name] => 1st Source Corporation
        [sector] => Finance
        [industry] => Major Banks
    )

[5] => Array
    (
        [symbol] => FUBC
        [name] => 1st United Bancorp Inc. (FL)
        [sector] => Finance
        [industry] => Major Banks
    )

[6] => Array
    (
        [symbol] => VNET
        [name] => 21Vianet Group Inc.
        [sector] => Technology
        [industry] => Computer Software: Programming
    )

[7] => Array
    (
        [symbol] => XXII
        [name] => 22nd Century Group Inc
        [sector] => Consumer Non-Durables
        [industry] => Farming/Seeds/Milling
    )

[8] => Array
    (
        [symbol] => DDD
        [name] => 3D Systems Corporation
        [sector] => Technology
        [industry] => Computer Software: Prepackaged Software
    )

[9] => Array
    (
        [symbol] => MMM
        [name] => 3M Company
        [sector] => Health Care
        [industry] => Medical/Dental Instruments
    )

)

1 个答案:

答案 0 :(得分:0)

我重写了insertCompaniesByRange()方法,现在它可以工作了。 这是它的外观:

public function insertCompaniesByRange($letters = array(), $number = null) {
    if (is_array($letters) && $number && count($letters) == 2) {
        $range = range($letters[0], $letters[1]);
        foreach ($range as $letter) {
            if ($this->getCompaniesByLetter($letter,$number)) {
                foreach($this->companyList as $company) {
                    $this->insertCompany($company);
                }
            }
        }
    }

    return false;
}