Web Analytics Made Easy -
StatCounter Deleting an element in an array - CodingForum

Announcement

Collapse
No announcement yet.

Deleting an element in an array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting an element in an array

    I have a value in a database that i seperate by commas and explode() into an array. Im trying to delete one element of the exploded array, implode() the array back into a string and update the database. Here is the code I tried.

    PHP Code:
    $array[$id] ="";
    implode(",",$array); 
    The problem is it still implodes the empy values back into the string. Is there an easy way to delete an element from an array without a for loop that shifts everything forward one?
    -bubbles

  • #2
    See this thread for your answer.. Some of it doesn't relate to what you need but buried in this thread is your answer as the original starter of the thread was having a similar problem.

    Comment


    • #3
      Use the unset() to remove the entry. Note it will re-number the index.
      Try the following snippet and you'll see.
      PHP Code:
      <?php
      $str 
      "A,B,C,D,E";
      $ar explode(","$str);
      $id 2;
      echo 
      '<pre>'print_r($ar); echo '<br>';
      unset(
      $ar[$id]);
      echo 
      '<pre>'print_r($ar); echo '<br>';
      $str implode(",",$ar);
      echo 
      $str;  
      ?>
      Ronald
      RTFM is an almost extinct art form, it should be subsidized.

      Comment

      Working...
      X