simple delete from array?

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

    #1

    simple delete from array?

    Any way to do a simple delete from array?

    In other words, what would be the *easiest* (and fastest php runtime)
    way to delete "banana" from the following array:

    $my_array = array(
    "apple",
    "banana",
    "grape",
    "lime"
    );

    .....so that $my_array will now be:

    $my_array = array(
    "apple",
    "grape",
    "lime"
    );

  • Shebang O'Reilly

    #2
    Re: simple delete from array?

    On Sat, 31 Jul 2004 04:44:31 GMT, Westcoast Sheri
    <sheri_deb88@no spamun8nospam.c om> wrote:
    [color=blue]
    >Any way to do a simple delete from array?
    >
    >In other words, what would be the *easiest* (and fastest php runtime)
    >way to delete "banana" from the following array:
    >
    >$my_array = array(
    >"apple",
    >"banana",
    >"grape",
    >"lime"
    >);
    >
    >....so that $my_array will now be:
    >
    >$my_array = array(
    >"apple",
    >"grape",
    >"lime"
    >);[/color]

    if you know the position (offset) of the element you want to delete
    (in your example above, 'banana' is element 1 in the array):

    array_splice($m y_array, 1, 1);

    if not:

    array_splice($m y_array, array_search('b anana', $my_array), 1);

    if you are using associative arrays you can use unset:

    $my_array = array(
    'apple' => 1
    'banana' => 2,
    'grape' => 3,
    'lime' => 4
    );
    unset($my_array['banana']);

    this appears not to work with non-associative arrays.

    hth,
    hc.


    Comment

    • Gary L. Burnore

      #3
      Re: simple delete from array?

      On Sat, 31 Jul 2004 04:44:31 GMT, Westcoast Sheri
      <sheri_deb88@no spamun8nospam.c om> wrote:
      [color=blue]
      >Any way to do a simple delete from array?
      >
      >In other words, what would be the *easiest* (and fastest php runtime)
      >way to delete "banana" from the following array:
      >
      >$my_array = array(
      >"apple",
      >"banana",
      >"grape",
      >"lime"
      >);
      >
      >....so that $my_array will now be:
      >
      >$my_array = array(
      >"apple",
      >"grape",
      >"lime"
      >);[/color]

      Use a second array and for each in the first array either insert it
      into the second array or not depending on your criteria.

      --
      gburnore@databa six dot com
      ---------------------------------------------------------------------------
      How you look depends on where you go.
      ---------------------------------------------------------------------------
      Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
      | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
      DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
      | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
      Black Helicopter Repair Svcs Division | Official Proof of Purchase
      =============== =============== =============== =============== ===============
      Want one? GET one! http://signup.databasix.com
      =============== =============== =============== =============== ===============

      Comment

      Working...