title: Can someone help me debug this error: Undefined offset: 0?
body: <p>I am creating a function that calculate the quantity of sold tickets, and here is my code:    </p>
  
  <pre><code>public function get_quantity($tickets_info){
      $i = 0; //$i is the starting point of the loop
      $k = 0;
      $Qty = 1; // this is used to save the quantity of tickets, by default there is always one ticket
      $index = array();
      $quantity = array();
      for($j = 1; $j &lt; count($tickets_info); $j++) {
  //            if the ticket_id are the same, then increase the quantity by one
          if($tickets_info[$i]['ticket_id'] == $tickets_info[$j]['ticket_id'])
          {
              $Qty++;
          }
  //            if the ticket_id are not the same, then push the quantity into an array and remember the index
          else
          {
              $idx = $j;//remember the index of the next first different ticket_id
              $i = $j;//find the next starting point
              $index[$k] = $idx;//push back the index of the next different ticket_id
              $quantity[$k] = $Qty;//save quantity into the array
              $k++;//increase the index poniter
              $Qty = 1;//reset quantity back to one
          }
      }
  //        push the last quantity into the array
      $quantity[$k+1] = $Qty;
  
      //assign the ticket information into a new array
      for($m = 0; $m &lt; count($quantity); $m++){
          $ticket[$m] = $tickets_info[$m]; 
      }
  
      //create the finally array, combine ticket information with quantity
      $n = 0;
      foreach($ticket as $row)
      {
          $row['Qty'] = $quantity[$n++];
      }
  
      return $ticket;
  
  }
  </code></pre>
  
  <p><code>$ticket_info</code> is a 2-D array which is generated from a SQL, and it has a structure like this:</p>
  
  <pre><code>$ticket_info
  (
      [0]=&gt;array
      (
          [ticket_id] =&gt; 0001
          [purchase_time] =&gt; 2014/01/02
          ....
  
      )
      [1]=&gt;array
      (
          [ticket_id] =&gt; 0001
          [purchase_time] =&gt; 2014/01/02
          ....
  
      )
      [2]=&gt;array
      (
          [ticket_id] =&gt; 0001
          [purchase_time] =&gt; 2014/01/02
          ....
  
      )
      ....
  )
  </code></pre>
  
  <p>Basically, if the tickets have the same <code>ticket_id</code> that means they have been bought at the same time (but in the database, I recorded them separately for a specific purpose), so I need to add them up and get the quantity.</p>
  
  <p>I'm not familiar with PHP arrays, so I write my algorithm in C++ and tested it. It works fine. However, when I tried to write the real code in PHP, I got 2 errors:</p>
  
  <p>For line, <code>$ticket[$m] = $tickets_info[$m];</code> Message: Undefined offset: 0</p>
  
  <p>For line, <code>$row['Qty'] = $quantity[$n++];</code>  Message: Undefined offset: 0</p>
  
  <p>I don't know why there is no index 0, maybe I didn't initialize the array correctly or I didn't pass the data in <code>$ticket_info</code> in the right format? Can someone help me look at this code?</p>