Solved

Remove element from an array.

Posted on 2007-03-28
4
813 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

19 Experts available now in Live!

Get 1:1 Help Now