remove element from array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • warner
    New Member
    • May 2022
    • 1

    remove element from array

    Hi, I want to remove one element from array so i'm using unset function but it also remove that index number. I want to re-index after removing array. is there any solution?
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Post your code... (please remember to format it using the [Code/] formatting tool in the ribbon)

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Try the array_splice() Function. In the following example, I remove 'black' from the Original Array and the Elements have appered to have been Re-indexed after the splice.
      Code:
      <?php
      $colors = Array("red", "green", "Blue", "purple", "orange", "black", "magenta", "cyan", "yellow", "violet", "brown");
      echo 'Number of Elements in $colors: ' . count($colors);
      
      echo "<BR><BR>";
      
      $colors_removed = array_splice($colors, 5, 1);
      echo 'Number of Elements in $colors_removed: ' . count($colors_removed) . "<BR>";
      
      echo $colors[0] . "<BR>";
      echo $colors[1] . "<BR>";
      echo $colors[2] . "<BR>";
      echo $colors[3] . "<BR>";
      echo $colors[4] . "<BR>";
      echo $colors[5] . "<BR>";
      echo $colors[6] . "<BR>";
      echo $colors[7] . "<BR>";
      echo $colors[8] . "<BR>";
      echo $colors[9] . "<BR>";
      Code:
      Number of Elements in $colors: 11
      
      Number of Elements in $colors_removed: 1
      red
      green
      Blue
      purple
      orange
      magenta
      cyan
      yellow
      violet
      brown

      Comment

      • paanjii2
        New Member
        • Jul 2022
        • 1

        #4
        pop - Removes from the End of an Array.
        shift - Removes from the beginning of an Array.
        splice - removes from a specific Array index.
        filter - allows you to programatically remove elements from an Array.

        Comment

        • dev7060
          Recognized Expert Contributor
          • Mar 2017
          • 655

          #5
          Using too many line breaks (<br>) is poor semantic HTML. Better use different logical blocks.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            @dev7060:
            Thanks for the pointer. Brand new at PHP, not my area of expertise (usually live in the Access/VBA Forum).

            Comment

            • Willjoe
              Banned
              New Member
              • Jul 2022
              • 12

              #7
              pop - Removes from the End of an Array.
              shift - Removes from the beginning of an Array.
              splice - removes from a specific Array index.
              filter - allows you to programmaticall y remove elements from an Array.

              Regards,
              Willjoe

              Comment

              Working...