0

this could have been asked over and over again but i have failed to get the best answer allover google. Please help me.

Problem

Am fetching data from a forex rates rest API and this is exactly how the reply looks when i print it out.

EUR/USD,1380039070258,1.34,868,1.34,873,1.34641,1.35193,1.34932
USD/JPY,1380039068699,98.,789,98.,797,98.471,99.180,98.838
GBP/USD,1380039067482,1.60,082,1.60,095,1.59546,1.60500,1.60419
EUR/GBP,1380039067816,0.84,245,0.84,256,0.84067,0.84495,0.84127
USD/CHF,1380039064893,0.91,161,0.91,172,0.90974,0.91338,0.91097
EUR/JPY,1380039066371,133.,236,133.,252,132.697,134.008,133.371
EUR/CHF,1380039063317,1.22,951,1.22,966,1.22853,1.23050,1.22919
USD/CAD,1380039062062,1.02,960,1.02,969,1.02655,1.03111,1.02841
AUD/USD,1380039069019,0.93,957,0.93,968,0.93635,0.94329,0.94307
GBP/JPY,1380039066561,158.,149,158.,170,157.342,158.978,158.552

Note that each line contains comma delimited data and the next line is on the next line.

When i store this data in a response variable $resp and use the code below

$result=str_getcsv($resp, "\n");
pr($result);

I get the expected result as below

Array
(
    [0] => EUR/USD,1380039070258,1.34,868,1.34,873,1.34641,1.35193,1.34932
    [1] => USD/JPY,1380039068699,98.,789,98.,797,98.471,99.180,98.838
    [2] => GBP/USD,1380039067482,1.60,082,1.60,095,1.59546,1.60500,1.60419
    [3] => EUR/GBP,1380039067816,0.84,245,0.84,256,0.84067,0.84495,0.84127
    [4] => USD/CHF,1380039064893,0.91,161,0.91,172,0.90974,0.91338,0.91097
    [5] => EUR/JPY,1380039066371,133.,236,133.,252,132.697,134.008,133.371
    [6] => EUR/CHF,1380039063317,1.22,951,1.22,966,1.22853,1.23050,1.22919
    [7] => USD/CAD,1380039062062,1.02,960,1.02,969,1.02655,1.03111,1.02841
    [8] => AUD/USD,1380039069019,0.93,957,0.93,968,0.93635,0.94329,0.94307
    [9] => GBP/JPY,1380039066561,158.,149,158.,170,157.342,158.978,158.552
    [10] => 
)

This works perfectly on my localhost server but when i deploy it, i get an error below

Call to undefined function str_getcsv()

It seams the server doesn't know about that str_getcsv() function i tried to use.

I tried to use other functions like fgetcsv() but cant get them to give me the array output i want.

below is how i used fgetcsv()

$fh = fopen('php://memory', 'rw'); 
fwrite($fh, $resp);
rewind($fh);
$result = fgetcsv( $fh, strlen($resp), ',', ' ');
fclose($fh); 
pr($result);

but it picks only the first line of the $resp string and here is the output it gives

Array
(
    [0] => EUR/USD
    [1] => 1380040007538
    [2] => 1.34
    [3] => 882
    [4] => 1.34
    [5] => 890
    [6] => 1.34641
    [7] => 1.35193
    [8] => 1.34932
)

yet i want all the lines of the $resp string indexed in the array like in the first array

Please if you can help me get this to work by converting the string to the desired array, thanks inadvance.

4

2 回答 2

1

您可以使用explode("\n", $resp)来获取行数组。

于 2013-09-24T16:26:32.187 回答
0

当他们告诉你检查文档或谷歌时,人们是对的。开发软件不仅仅是输入代码,也是阅读和研究的重要组成部分。

您的服务器产生的错误很可能是因为您运行了一个古老的 php 版本并且应该更新它 - 安全性也是这里的一个关键字。因为这个函数http://php.net/manual/en/function.str-getcsv.php从 5.3 开始支持。

此外,那里的第一条评论还包括一个示例,说明如何在没有该功能的情况下存档相同的内容:

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}

我没有测试过,那是你的工作。

如果缺少该功能,则之后的注释会进行更详细的替换。

于 2013-09-24T16:31:17.143 回答