php - Covert comma delimited string to array like a csv string without using str_getcsv function -
this have been asked on , on again have failed best answer allover google. please me.
problem
am fetching data forex rates rest api , how reply looks when print 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 each line contains comma delimited data , next line on next line.
when store data in response variable $resp
, use code below
$result=str_getcsv($resp, "\n"); pr($result);
i expected result 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 on localhost server when deploy it, error below
call undefined function str_getcsv()
it seams server doesn't know str_getcsv()
function tried use.
i tried use other functions fgetcsv()
cant them give me array output
want.
below how used fgetcsv()
$fh = fopen('php://memory', 'rw'); fwrite($fh, $resp); rewind($fh); $result = fgetcsv( $fh, strlen($resp), ',', ' '); fclose($fh); pr($result);
but picks first line of $resp
string , here output 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 want lines of $resp
string indexed in array in first array
please if can me work converting string desired array, inadvance.
you can use explode("\n", $resp)
array of lines.
Comments
Post a Comment