PHP Error: Undefined offset error within foreach loop-Collection of common programming errors

I have a csv file which I am trying to turn into a different structured array. First, I turn it into an array named all_data() constructed like this:

$data = file_get_contents($id . '.csv');
      $data_array = explode("\n", $data);
      foreach($data_array AS $data){
            $all_data[] = explode("\t", $data);
      }

results look like this:

    array(5) {
      [0]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }
      [1]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }
      [2]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }
      [3]=>
      array(2) {
        [0]=>
        string(10) "2012-11-14"
        [1]=>
        string(2) "10"
      }

      [4]=>
      array(1) {
        [0]=>
        string(0) ""
      }

}

And then I turn it into im_arr() with the following code:

  foreach($all_data as $key => $value){
            $im_arr[$key][$value[0]] = $value[1];
       }

The results:

array(5) {
  [0]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }
  [1]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }
  [2]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }
  [3]=>
  array(1) {
    ["2012-11-14"]=>
    string(2) "10"
  }

  [4]=>
  array(1) {
    [""]=>
    NULL
  }
}

And then, finally another foreach loop gives me the results I am looking for:

foreach ($im_arr as $val) {
    foreach ($val as $key => $val2) {
        $im_data[$key]=$val2;
    }
       }

With the result for im_data() being:

array(2) {
  ["2012-11-14"]=>
  string(2) "10"
  [""]=>
  NULL
}

Which would be perfect, since the array im_data() is exactly what I would like to get out of all_data(). However, when I am trying to put this code in another part of the program it doesn’t work, and I am thinking it might be because of the warnings I receive:

“PHP Notice: Undefined offset: 1 in … on line 93”

Line 93 corresponds to this line:

$im_arr[$key][$value[0]] = $value[1];

Here is the complete part of the code:

  $all_data = array();
  $im_arr=array();

$data = file_get_contents($id . '.csv');
      $data_array = explode("\n", $data);
      foreach($data_array AS $data){
            $all_data[] = explode("\t", $data);
      }

      foreach($all_data as $key => $value){
            $im_arr[$key][$value[0]] = $value[1];  //the line for the error
       }
    $im_data=array();  

foreach ($im_arr as $val) {
    foreach ($val as $key => $val2) {
        $im_data[$key]=$val2;
    }
       }




var_dump($im_data);

I know there are many many questions posted for this same error, but I couldn’t figure out the problem with this particular piece of code.

  1. This is the problem:

    [4]=>
      array(1) {
        [0]=>
        string(0) ""
      }
    

    Just check that the data is set, and isn’t empty before adding them to $im_arr:

    foreach ($all_data as $key => $value) { 
      if (isset($value[0]) && isset($value[1]) && !empty($value[0]) && !empty($value[1])) {
        $im_arr[$key][$value[0]] = $value[1];
      }
    }
    
  2. For every foreach i would pre-check if the first argument is an array

    For instance ;

    //Just add line below for every foreach (and add any required else statement if needed)
    if(is_array($im_arr))
    foreach ($im_arr as $val) {
        if(is_array($val))
        foreach ($val as $key => $val2) {
            $im_data[$key]=$val2;
        }
    }
    

Originally posted 2013-11-27 12:24:51.