Array Question I am stuck on this one

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nitronic

    Array Question I am stuck on this one



    Lets say you have a array declared like this


    <?php

    $Array=array("D rum & Bass","Jungle", "House","Techno ","Hip Hop");

    ?>



    but what i want do do is somehow using a for loop to remove items in the
    array. The items i want to remove are 0,2,4 or just 2,5. Deleting a item
    in the array has to be flexible at which item to delete.


    I appreciate anybody helps me on this thanks very much :)



  • ZeldorBlat

    #2
    Re: Array Question I am stuck on this one

    To remove item $Array[4]:

    unset($Array[4]);

    Note that this will not re-index the array continuously -- you'll just
    have buckets with null values.

    If you want to re-index the array, use:

    array_merge($Ar ray);

    Comment

    • Nitronic

      #3
      Re: Array Question I am stuck on this one


      Thanks but what if this were the case

      <?php

      //Items in Array One
      $Array=array("D rum & Bass","Jungle", "House","Techno ","Hip Hop");

      //Remove Array

      $Array_removeIt ems=array("0"," 2","4");

      ?>

      How would i remove items in Array One with the items specified in the other
      array. Note in Array_removeIte ms the values can change also.

      Thanks a million :)



      Hello ZeldorBlat,
      [color=blue]
      > To remove item $Array[4]:
      >
      > unset($Array[4]);
      >
      > Note that this will not re-index the array continuously -- you'll just
      > have buckets with null values.
      >
      > If you want to re-index the array, use:
      >
      > array_merge($Ar ray);
      >[/color]


      Comment

      • ZeldorBlat

        #4
        Re: Array Question I am stuck on this one

        //Items in Array One
        $Array=array("D rum & Bass","Jungle", "House","Techno ","Hip Hop");

        //Remove Array
        $Array_removeIt ems=array("0"," 2","4");

        for($i = 0; $i < count($Array_re moveItems); $i++)
        unset($Array[$Array_removeIt ems[$i]]);

        Comment

        • Robin

          #5
          Re: Array Question I am stuck on this one

          ZeldorBlat wrote:[color=blue]
          > //Items in Array One
          > $Array=array("D rum & Bass","Jungle", "House","Techno ","Hip Hop");
          >
          > //Remove Array
          > $Array_removeIt ems=array("0"," 2","4");
          >
          > for($i = 0; $i < count($Array_re moveItems); $i++)
          > unset($Array[$Array_removeIt ems[$i]]);
          >[/color]

          or

          foreach ($Array_removeI tems as $i)
          unset($Array[$i]);

          (still surprised by the number of times I see this C-style for loop
          instead of a foreach loop)

          Comment

          Working...