I have an array that always ends up with 1 blank entry at the end.

What's the most efficient way of deleting the last element of an array?

The only way I can think of is to create a counter, subract 1 and do an if loop.

That seems like overkill though.

    You might, should take a look at how you are building your array too, to see why the blank entry is being put into the array... You could probably rewrite that portion of your code to avoid it pretty easily, so your array just never has that blank value in it at the end... If you want some help with that, post your code, and give an explanation of what you are doing/wanting to do, and we can help you tweak it.

      actually I figured it out. The hidden element wasn't coming from the array at all. The slice_array function showed me that. It removed the last thing but the blank object was still there.

      I found out because of that the blank page (I'm making pdf's) was coming from elsewhere in the script (addpage). I fixed it like this. Would you ahve done different?

      $count = count($checkbox);
      $i=1;
      foreach ($checkbox as $value){
      
      # Prepare fonts
      $f1 = $pdf->SetFont('Courier');
      
          # Draw Invoice Box
          $pdf->Cell(50,8,'INVOICE NUMBER',1);
          $pdf->LN();
          $pdf->Cell(50,8,$value,1);
      
          if ($i != $count){
                  $pdf->AddPage();
                  $i++;
          }
      
      
      }
      $pdf->Output();

        You could have used [man]array_pop/man if you really did need to remove the last element from an array.

          Hey Butthead, that looks good. There are only so many ways you can do it... At least you got rid of the blank value while making the array... Much cleaner. Be sure to mark the thread resolved.

            This is a bit more efficient

             <?php
            $count = count($checkbox);
            for ($i=0;$i<$count;$i++) {
                # Prepare fonts
                $f1 = $pdf->SetFont('Courier');
            
            # Draw Invoice Box
            $pdf->Cell(50,8,'INVOICE NUMBER',1);
            $pdf->LN();
            $pdf->Cell(50,8,$checkbox[$i],1);
            $pdf->AddPage();
            } //end for
            $pdf->Output();
            ?>

            This gains in efficiency because you're not executing an if statement everytime through the loop.

            NOTE: If the blank page comes back just change $i<$count to $i<($count-1)

              Write a Reply...