Hi Bob

> I have a standard array:
> $foo = array(
> 0 => 'abc'
> 1 => 'def'
> 2 => 'ghi');
>
> and I have a variable, $bar, which equals 'def' ... and I
> want to check
> my array to see if the value of $bar exists in the array, and if it
> does, delete it from the array.
>
> if (in_array($bar, $foo)) {
>    // what goes here???
> }

ARRAY_SPLICE($foo, position('def'), 1);

To get the position you will have to cycle through the array though:
$index = 0;
if (in_array($bar, $foo)) {
  foreach ($foo as $val) {
    if ($val==$bar) {array_splice($foo, $index, 1);}
    $index ++;
  }
}

If the value can exist several times in the array you will have to take this
into account though:

$index = 0;
$deleted = 0;
if (in_array($bar, $foo)) {
  foreach ($foo as $val) {
    if ($val==$bar) {
      array_splice($foo, ($index - $deleted), 1);
      $deleted ++;
    }
    $index ++;
  }
}

simpler maybe:

if (in_array($bar, $foo)) {
  foreach ($foo as $val) {
    if ($val==$bar) {array_splice($foo, pos($foo), 1);}
  }
}

But actually used the one above, never tested if pos() also works when not
using prev(), next() and reset() to walk through an array.

Marc



Community email addresses:
  Post message: php-list@yahoogroups.com
  Subscribe:    [EMAIL PROTECTED]
  Unsubscribe:  [EMAIL PROTECTED]
  List owner:   [EMAIL PROTECTED]

Shortcut URL to this page:
  http://groups.yahoo.com/group/php-list 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-list/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to