2014-01-16 72 views
0

即時通訊工具在SEMRUSH api上的項目和結果來到$ field1; field2如此分隔;PHP陣列和API的問題

當前結果我得到了這樣5 reslts彼此相鄰基於其列,不過,我需要第二陣列僅

 if (isset($_POST['search_term'])) { 

     $kwtext1 = str_replace(" ", '+', ($_POST['search_term'])); 
     $kwtext = str_replace("\r", '', trim($kwtext1)); 
     $kwdata = array(); 
     $kwlines = explode("\n", $kwtext); 

     $db = "us"; 
     $limit = "10"; 
     } 


    foreach ($kwlines as $kw) { 

    usleep(100); 

    $u = 'http://' . $db . '.api.semrush.com/?action=report&type=phrase_this&phrase=' . $kw . '&key=' . $key . '&display_limit=' . $limit . '&display_offset=0&export=api&export_columns=Ph,Nq,Cp,Co,Nr'; 

     $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $u); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

     $answer  = curl_exec  ($ch); 

    $kwdata = explode ("\n", trim ($answer)); 
    $kwfields = explode (";", array_shift ($kwdata)); 

     if (count ($kwdata) > 0) 
     { 
    ?> 
    <table class="output"> 
    <tr><th>Domain 1</th> 
    <?php $csv_output .= ' ' . ", " . 'Domain' . ", ";?> 

    <? 
     foreach ($kwfields as $field) 
     { 
    ?> 
     <th><?= $field; ?></th> 
    <?php $csv_output .= $field . ", ";?> 
    <? 
     } 
    ?> 
    </tr> 
    <? 
     foreach ($kwdata as $dataline) 
     { 
      $values = explode (";", $dataline, count ($kwfields)); 
    ?> 
    <tr><td><? echo $kw ?></td> 
    <?php $csv_output .= $kw . ", ";?> 
    <? 
      foreach ($values as $value) 
      { 
    ?> 
     <td><?= $value; ?></td> 
    <?php $csv_output .= $value . ", ";?> 
    <? 
      } 
    ?> 
    </tr> 
      <? 
     } 
     ?> 
     </table> 
    <? 
     } 
     else 
     { 
     ?> 
      No data found for your request 
     <? 
       } 
    } 


     ?> 
+0

你的問題到底是什麼? – ohmusama

回答

1

如果你只是想第二個結果,你知道你將不得不5個結果,那麼我想改變行:

$kwfields = explode(";", array_shift($kwdata)); 

到:

$kwfields = explode(";", $kwdata[1]); 

會工作。

但是,如果您沒有至少2行數據,將會失敗。你保證得到2或更多?如果不是,那麼你需要檢查一下。

+0

它不返回任何東西 – user3159221

+0

萊伊斯說你想在這裏得到第二個結果foreach($ values as $ value) – user3159221

+0

'foreach($ values as $ i => $ value){if($ i == 1) {/ * stuff * /}}' – ohmusama