E
E
Evgeny Shevtsov2018-03-19 18:38:05
PHP
Evgeny Shevtsov, 2018-03-19 18:38:05

How to convert tsv response from yandex direct api to array?

Actually the question is this.
I receive a Yandex Direct report

"a259784,259785 (19/3/2017 - 19.03.2018)"
AdGroupName Impressions Clicks The Cost
Group №2952489 10,905,034 939 944 575
118 050 000 Group 944 645 576 994 320 000 10976444 №2952490 Group №2952491 10,926,208 947 145 580 294 830 000
Group 945 256 574 691
690 000 10851243 №2952492
Group №2952493 10,844,346 946921 578424030000
Total rows: 5

How can I convert it to an array?
So that the values ​​correspond to the keys ?
'AdGroupName' => 'Группа №2952489' , Impressions	=> '10905034',  ........

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Shevtsov, 2018-03-19
@Rattlesneyk

Slightly modified the answer Vladimir Varlamov
Thank you!

$str = preg_replace("/Total rows:(.*)/", "", $str);  
$str =  explode("\n", $str) ;
unset($str[0]);
unset($str[1]);
 
foreach($str as $line){
  if(!empty($line)){
    list($gn, $imp, $cl, $coast) = explode("\t", $line);
    $linearray = array(
        "AdGroupName" => $gn,
        "Imp" => $imp,
        "clicks" => $cl,
        "coast" => $coast,
    );
  print_R($linearray);
  }
};

G
GarrySeldon, 2022-01-21
@GarrySeldon

class TsvParce 
{
    private $totals;
    private $data;
    private $headers;
    private $str;

    public function __construct($str)
    {
        $this->str = $str;
        $this->parce();
        return $this;
    }

    public function getTotal()
    {
        return $this->totals;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getHeaders()
    {
        return $this->headers;
    }
     
    public function parce()
    {
        if (!$this->str || strlen($this->str) < 10) {
            return $this;
        }
        $lines = explode(PHP_EOL, $this->str);
        $count = count($lines);
        $lastLine = $count - 2;
        $totalLine = $lines[$lastLine];
        $this->parceTotal($totalLine);
        $this->headers = explode("\t", $lines[1]);
        unset($lines[0], $lines[1], $lines[$lastLine], $lines[$count - 1]);
        if (count($this->headers) > 0) {
            $this->parceData($lines);
        }
        return $this;
    }

    private function parceData($lines)
    {
        $countHeader = count($this->headers);
        $data = [];
        foreach ($lines as $line) {
            $row = explode("\t", $line);
            if (count($row) == $countHeader) {
                $data[] = array_combine($this->headers, $row);
            }
        }
        $this->data = $data;
    }

    private function parceTotal($line)
    {
        if (preg_match('/Total rows: (\d+)/', $line, $match)) {
            $this->totals = $match[1];
        }
    }
}

//Пример использования
    $tsv = new TsvParce($data);
    print_r($tsv->getTotal());
    print_r($tsv->getHeaders());
    print_r($tsv->getData());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question