0

I have csv text as like below

Keyword;Search Volume;CPC;Competition;Number of Results;Trends
shoes of ladies;90;0.00;0.00;96700000;0.11,0.11,0.11,0.22,0.11,0.11,1.00,0.11,0.11,0.11,0.11,0.11
ladys shoes;110;0.95;1.00;96800000;0.79,0.64,0.50,0.79,1.00,0.79,0.79,0.79,0.64,0.50,0.64,1.00
kadies shoes;90;1.16;1.00;116000000;0.44,1.00,1.00,0.11,1.00,0.78,0.56,0.11,0.56,0.33,0.33,1.00
shoes ladies;390;1.20;1.00;119000000;0.54,0.54,0.54,0.81,1.00,1.00,0.81,0.67,1.00,0.67,0.67,0.81
shoes for ladies;140;1.38;1.00;132000000;0.42,0.27,0.35,0.42,0.54,0.65,0.42,0.27,0.42,0.81,0.65,1.00
laddies shoes;90;2.23;1.00;76800000;0.22,0.33,0.11,0.44,0.11,0.78,0.33,0.22,0.78,0.11,0.11,1.00
ladies shoe;90;1.50;1.00;52400000;0.45,0.64,0.82,0.82,1.00,1.00,1.00,1.00,0.82,0.64,0.82,0.82
ladies shoes uk;2900;1.19;1.00;34700000;0.43,0.66,0.82,0.82,0.82,1.00,0.66,0.66,0.55,0.55,0.66,0.66
ladies black shoes;590;1.31;1.00;63600000;0.67,0.67,0.67,0.82,0.67,0.82,0.67,0.82,0.67,0.82,0.82,1.00
black ladies shoes;320;1.34;1.00;131000000;0.67,0.67,0.67,0.67,0.67,0.82,0.67,0.67,0.82,0.82,1.00,1.00

and in this ; deliminator seperates my data . and the very first line was my header Keyword;Search Volume;CPC;Competition;Number of Results;Trends so total 6 headers .

i am trying to get the array like as below

array(12) {
  [0]=>
  array(6) {
    ["Keyword"]=>"shoes of ladies"
    ["Search Volume"]=>"90"
    ["CPC"]=> "0.00"
    ["Competition"]=> "0.11"
    ["Number of Results"]=> "96700000"
    ["Trends"]=>
        array(){
        [0]=>0.11,
        [1]=>0.11,
        [2]=>0.11
        ...
        []
        }

  [1]=>
  array(6) {
    ["Keyword"]=>"ladys shoes"
    ["Search Volume"]=>"90"
    ["CPC"]=> "0.00"
    ["Competition"]=> "0.11"
    ["Number of Results"]=> "96700000"
    ["Trends"]=>
        array(){
        [0]=>0.11,
        [1]=>0.11,
        [2]=>0.11
        ...
        []
        }
  }

i was trying with code

$url = "Keyword;Search Volume;CPC;Competition;Number of Results;Trends
shoes of ladies;90;0.00;0.00;96700000;0.11,0.11,0.11,0.22,0.11,0.11,1.00,0.11,0.11,0.11,0.11,0.11
ladys shoes;110;0.95;1.00;96800000;0.79,0.64,0.50,0.79,1.00,0.79,0.79,0.79,0.64,0.50,0.64,1.00
kadies shoes;90;1.16;1.00;116000000;0.44,1.00,1.00,0.11,1.00,0.78,0.56,0.11,0.56,0.33,0.33,1.00
shoes ladies;390;1.20;1.00;119000000;0.54,0.54,0.54,0.81,1.00,1.00,0.81,0.67,1.00,0.67,0.67,0.81
shoes for ladies;140;1.38;1.00;132000000;0.42,0.27,0.35,0.42,0.54,0.65,0.42,0.27,0.42,0.81,0.65,1.00
laddies shoes;90;2.23;1.00;76800000;0.22,0.33,0.11,0.44,0.11,0.78,0.33,0.22,0.78,0.11,0.11,1.00
ladies shoe;90;1.50;1.00;52400000;0.45,0.64,0.82,0.82,1.00,1.00,1.00,1.00,0.82,0.64,0.82,0.82
ladies shoes uk;2900;1.19;1.00;34700000;0.43,0.66,0.82,0.82,0.82,1.00,0.66,0.66,0.55,0.55,0.66,0.66
ladies black shoes;590;1.31;1.00;63600000;0.67,0.67,0.67,0.82,0.67,0.82,0.67,0.82,0.67,0.82,0.82,1.00
black ladies shoes;320;1.34;1.00;131000000;0.67,0.67,0.67,0.67,0.67,0.82,0.67,0.67,0.82,0.82,1.00,1.00" ;


$lines = explode("\n", $url);

$head = str_getcsv(array_shift($lines));

$data = array();
foreach ($lines as $line) {
$data[] = array_combine($head, str_getcsv($line));
}

so its not giving me the desired output. any suggestions please

2
  • Can't you download the data file other format ? Like JSON or so ? Commented Feb 22, 2017 at 9:55
  • actually its coming from external api URL . @pinoyCoder Commented Feb 22, 2017 at 10:02

2 Answers 2

2

This should do the trick for you:

$head = str_getcsv(array_shift($lines), ";");
$data = array();
foreach ($lines as $line) {
   $tmp = array_combine($head, str_getcsv($line, ";"));
   $tmp['Trends'] = explode(",", $tmp['Trends'])
   $data[] = $tmp;
}

This is the explanation: As described here http://php.net/manual/en/function.str-getcsv.php, you forgot to set the correct delemiter. Which is by default ',' and you need ';'.

Sign up to request clarification or add additional context in comments.

2 Comments

But the array is not coming my desire way. any suggestions ?
I have changed my answer it a little bit.
1

Try this:

    $lines = explode("\n", $url);

    $head = str_getcsv(array_shift($lines),";");

    $data = array();
    foreach ($lines as $line) {
        $tempArr = str_getcsv($line,";");
        $tempArr[5] = explode(",",$tempArr[5]);
        $data[] = array_combine($head, $tempArr);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.