Solved

Remove element from an array.

Posted on 2007-03-28
4
825 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:Richard Quadling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Cornelia Yoder
Cornelia Yoder 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:Richard Quadling
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 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