Removing a specific element in an array

7

I have the following array

$input = array("item 1", "item 2");

But since it is dynamic, items can change

$input = array("item 2", "item 4");

$input = array("item 4");

Is it possible to use array_splice to remove, if it exists, a specific element (item 2) from the array?

    
asked by anonymous 01.08.2014 / 19:36

4 answers

15

If you know the position in the array you can use the unset (); directly

unset($input[0]);

To be clearer:

$input = array("item 2", "item 4");
var_dump($input);
unset($input[0]);
var_dump($input);

Gives:

array(2) {
  [0]=>
  string(6) "item 2"
  [1]=>
  string(6) "item 4"
}
array(1) {
  [1]=>
  string(6) "item 4"
}

If you do not know the position in the array you must go through the array or use array_search () first and then unset()

$key = array_search('item 2', $input);
if($key!==false){
    unset($input[$key]);
}

Example:

$input = array("item 2", "item 4");
$key = array_search('item 2', $input);
if($key!==false){
    unset($input[$key]);
}
var_dump($input);

What gives

array(1) {
  [1]=>
  string(6) "item 4"
}
    
01.08.2014 / 19:37
4

You can use "array_diff" to remove one or more elements from an array by the values:

$input = array("item 1", "item2", "item3", "item4");

$remover = array("item2");

$resultado = array_diff($input, $remover)

$ result would be an array with only "item 1", "item3", "item4".

But this is only valid if you want to remove any occurrence of the same value in your array, that is, if there are two elements with "item2", both will be removed.

    
01.08.2014 / 19:51
1

And if the array you are looking for is multidimensional, a answer to the @Sergio answer would be a function originated in the comments of the PHP manual (but today, unfortunately lost) that allows search recursively using the PHP Iterators themselves:

/**
 * Searches value inside a multidimensional array, returning its index
 *
 * Original function by "giulio provasi" (link below)
 *
 * @param mixed|array $haystack
 *   The haystack to search
 *
 * @param mixed $needle
 *   The needle we are looking for
 *
 * @param mixed|optional $index
 *   Allow to define a specific index where the data will be searched
 *
 * @return integer|string
 *   If given needle can be found in given haystack, its index will
 *   be returned. Otherwise, -1 will
 *
 * @see http://www.php.net/manual/en/function.array-search.php#97645 (dead)
 */
function search( $haystack, $needle, $index = NULL ) {

    if( is_null( $haystack ) ) {
        return -1;
    }

    $arrayIterator = new \RecursiveArrayIterator( $haystack );

    $iterator = new \RecursiveIteratorIterator( $arrayIterator );

    while( $iterator -> valid() ) {

        if( ( ( isset( $index ) and ( $iterator -> key() == $index ) ) or
            ( ! isset( $index ) ) ) and ( $iterator -> current() == $needle ) ) {

            return $arrayIterator -> key();
        }

        $iterator -> next();
    }

    return -1;
}

To use just inform the array where to look and what you are looking for. If the array structure is more or less known, you can enter a third argument so that the search is restricted only to the subroutines with that key.

    
02.08.2014 / 02:13
0

Well I was looking for a solution to something similar and what I did to solve was to scan the array in a foreach and remove the variable found with unset (); and create a new '$ result' array without this variable. An example below.

$input = array 0 => ("item1", "item2", "item3", "item4")
         array 1 => ("item1", "item2", "item3", "item4")

$contador=0;

foreach ($input as $i):
        if(isset($i['item1'])): unset($i['item1']);  endif;
        $resultado[$contador++]=$i;
endforeach;

$resultado = array 0 => ( "item2", "item3", "item4")
             array 1 => ( "item2", "item3", "item4")

Obs. I am new aki and in programming tbm I believe that is not the best solution but solved in the case;)

    
19.05.2018 / 03:47