Question

Trying to deconstruct a function I'm receiving a warning notice on that is used to retrieve CSV data from a database.

The function is called from index.php file, and may be formatted as

$masterRecords = $qb->genResultsTable($qid, $config[27]);

$config[27] is set dynamically, but to help understand; it is a period-delimited list of whole numbers. e.g. "3.4.5.6.7.8.9.25.141.137.83"
$qid is a whole number - e.g. "63".

The function works, but I'd like to see if I can get rid of the php Notice.
Getting notices for every item passed through the loop :

Notice: Undefined offset: 106 in /vagrant/public/arc/inc/lib.php on line 522

Call Stack:

1   0.0028  417156  {main}()    ../index.php:0
2   1.2886  473280  qb->genResultsTable()   ../index.php:66

The function:

public function genResultsTable($qid, $clist) {
  $url = "{$this->qb_ssl}{$this->db_id}?act=API_GenResultsTable&ticket={$this->ticket}&apptoken={$this->app_token}&qid={$qid}&clist={$clist}&slist={$clist}&options=csv";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cjFile);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $this->ckfile);
  //  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  curl_setopt($ch, CURLOPT_COOKIE, "TICKET=" . urlencode($this->ticket));
  $r = curl_exec($ch);
  $r = preg_replace_callback('/([\'"])([^"]+)\1/', 'call_back', $r);
  preg_match_all('/(.*)\r\n/', $r, $matchs);
  $fields = explode('.', $clist);
  $count = count($matchs[0]);
  $arrs = array();
  for ($i = 1; $i < $count; $i++) {
    $explode_arr = explode(',', $matchs[0][$i]);
    $arr = array();
    foreach ($fields as $key => $field) {
  // vv THIS BELOW LINE IS THE LINE THAT IS INDICATED IN ERROR vv
      $arr[$field] = urldecode($explode_arr[$key]);
    }
    $arrs[] = $arr;
  }
  return $arrs;
}

function call_back($matches) {
  return urlencode($matches[0]);
}

No correct solution

OTHER TIPS

In order to make @HamZa's answer more clear, basically the reason why you are getting a notice is because you are accessing an array with an index that does not exist, or is not set...

Ideally, each time you use the [ ] with a variable key, it's good idea to test if the key index/key exists for that array.

So, simply prepend doSomeThing($arr[$x]) with

if(isset($arr[$x])){
     doSomeThing($arr[$x])
}

This will prevent invalid array access and the notice should go away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top