Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remove element from an array.

Posted on 2007-03-28
4
Medium Priority
?
829 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 668 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 668 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 664 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

Industry Leaders: 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 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