Solved

Remove element from an array.

Posted on 2007-03-28
4
820 Views
Last Modified: 2013-12-13
Cannot see the wood for the trees, so this is going to be some EASY points!

I have a dynamic array; i.e. at RUNTIME, values are added and removed.

Well, added is OK.

$a_Array[] = 'value';

By using in_array() I know if the value is already in the array.

How do I remove the value from the array? I have a stupidly convoluted way...

Get the key of the value and then unset that element.

<?php
$a = array('one','two',4 => 'three', 3 => 'four'); // NOT a typo!!!!
unset($a[reset(array_keys($a, 'three', True))]);
var_export($a);
?>

This is OK, the output is ...

array (
  0 => 'one',
  1 => 'two',
  3 => 'four',
)

but ...

Strict Standards: Only variables should be passed by reference on line 3

So, I suspect there is really a array_remove(&$array, $value) function SOMEWHERE.
0
Comment
Question by:RQuadling
4 Comments
 
LVL 14

Accepted Solution

by:
Aamir Saeed earned 167 total points
ID: 18807338
try something like

/**
    * Remove a value from a array
    * @param string $val
    * @param array $arr
    * @return array $array_remval
    */
    function array_remval($val, &$arr)
    {
          $array_remval = $arr;
          for($x=0;$x<count($array_remval);$x++)
          {
              $i=array_search($val,$array_remval);
              if (is_numeric($i)) {
                  $array_temp  = array_slice($array_remval, 0, $i );
                $array_temp2 = array_slice($array_remval, $i+1, count($array_remval)-1 );
                $array_remval = array_merge($array_temp, $array_temp2);
              }
          }
          return $array_remval;
    }

$stack=Array('apple','banana','pear','apple', 'cherry', 'apple');
array_remval("apple", $stack);

//output: Array('banana','pear', 'cherry')
0
 
LVL 11

Assisted Solution

by:elfe69
elfe69 earned 167 total points
ID: 18807374
Using unset is the standard way to remove an item from an array according to the documentation:

http://ch2.php.net/types.array

0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 166 total points
ID: 18807393
Use array_diff( ) or one of it's variations (depending on exactly how you want to specify the element to be removed).

http://us3.php.net/manual/en/function.array-diff.php

0
 
LVL 40

Author Comment

by:RQuadling
ID: 18807630
Doh.

Ok. That was WAY too easy!

/**
  * array array_remove ( array input, mixed search_value [, bool strict] )
  **/
function array_remove(array &$a_Input, $m_SearchValue, $b_Strict = False)
      {
      $a_Keys = array_keys($a_Input, $m_SearchValue, $b_Strict);
      foreach($a_Keys as $s_Key)
            {
            unset($a_Input[$s_Key]);
            }
      return $a_Input;
      }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php date - compare 4 40
Form Processing in PHP 11 31
datetime in sql 6 24
PHP: concatenate query 12 35
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now