0

im working on a project on SEMRUSH api and results are coming trhough in $field1;field2 so seperated by ;

current result i get has so 5 reslts next to each other based on its columns, however i need second array only

     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
       <?
              }
     }


      ?>
1
  • what exactly is your question? Commented Jan 16, 2014 at 23:45

1 Answer 1

1

if you just want the second result, and you KNOW you are going to have 5 results, then I suppose changing line:

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

to:

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

will work.

But if you don't have at least 2 rows of data this will fail. Are you guaranteed to get 2 or more? if not then you will need to have a check around this.

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

3 Comments

it doesnt return anything eitherways
Leys say you wanna get second result here foreach ( $values as $value ) { }
foreach($values as $i => $value) { if ($i == 1) { /* stuff */ } }

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.