title: PHP Error: Undefined offset error within foreach loop
body: <p>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:</p>
  
  <pre><code>$data = file_get_contents($id . '.csv');
        $data_array = explode("\n", $data);
        foreach($data_array AS $data){
              $all_data[] = explode("\t", $data);
        }
  </code></pre>
  
  <p>results look like this:</p>
  
  <pre><code>    array(5) {
        [0]=&gt;
        array(2) {
          [0]=&gt;
          string(10) "2012-11-14"
          [1]=&gt;
          string(2) "10"
        }
        [1]=&gt;
        array(2) {
          [0]=&gt;
          string(10) "2012-11-14"
          [1]=&gt;
          string(2) "10"
        }
        [2]=&gt;
        array(2) {
          [0]=&gt;
          string(10) "2012-11-14"
          [1]=&gt;
          string(2) "10"
        }
        [3]=&gt;
        array(2) {
          [0]=&gt;
          string(10) "2012-11-14"
          [1]=&gt;
          string(2) "10"
        }
  
        [4]=&gt;
        array(1) {
          [0]=&gt;
          string(0) ""
        }
  
  }
  </code></pre>
  
  <p>And then I turn it into im_arr() with the following code:</p>
  
  <pre><code>  foreach($all_data as $key =&gt; $value){
              $im_arr[$key][$value[0]] = $value[1];
         }
  </code></pre>
  
  <p>The results:</p>
  
  <pre><code>array(5) {
    [0]=&gt;
    array(1) {
      ["2012-11-14"]=&gt;
      string(2) "10"
    }
    [1]=&gt;
    array(1) {
      ["2012-11-14"]=&gt;
      string(2) "10"
    }
    [2]=&gt;
    array(1) {
      ["2012-11-14"]=&gt;
      string(2) "10"
    }
    [3]=&gt;
    array(1) {
      ["2012-11-14"]=&gt;
      string(2) "10"
    }
  
    [4]=&gt;
    array(1) {
      [""]=&gt;
      NULL
    }
  }
  </code></pre>
  
  <p>And then, finally another foreach loop gives me the results I am looking for:</p>
  
  <pre><code>foreach ($im_arr as $val) {
      foreach ($val as $key =&gt; $val2) {
          $im_data[$key]=$val2;
      }
         }
  </code></pre>
  
  <p>With the result for im_data() being:</p>
  
  <pre><code>array(2) {
    ["2012-11-14"]=&gt;
    string(2) "10"
    [""]=&gt;
    NULL
  }
  </code></pre>
  
  <p>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: </p>
  
  <p>"PHP Notice:  Undefined offset: 1 in ... on line 93"</p>
  
  <p>Line 93 corresponds to this line: </p>
  
  <pre><code>$im_arr[$key][$value[0]] = $value[1];
  </code></pre>
  
  <p>Here is the complete part of the code:</p>
  
  <pre><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 =&gt; $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 =&gt; $val2) {
          $im_data[$key]=$val2;
      }
         }
  
  
  
  
  var_dump($im_data);
  </code></pre>
  
  <p>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. </p>