Removing a specific element in an array

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?

 10
Author: marcelo2605, 2014-08-01

5 answers

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 have to scroll 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);

Which gives

array(1) {
  [1]=>
  string(6) "item 4"
}
 21
Author: Sergio, 2014-08-01 17:49:24

You can use the "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)

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

But this only applies if you want to remove any and all occurrences of the same value in your array, i.e. if there are two elements with "item2", both will be removed.

 6
Author: Blau, 2014-08-01 17:51:31

And if the array where the search is multidimensional, a complement to @Sergio's response, would be a function originated in the comments of the PHP manual (but today, unfortunately lost) that allows the 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 it is enough to inform the array where to search and what to search. If the structure of the array is more or less known, you can enter a third argument so that the search is restricted only to sub-arrays with that key.

 1
Author: Bruno Augusto, 2017-04-13 12:59:32

Good was looking for a solution to something similar and what I did to solve was sweep the array in a foreach and remove the variable found with unset (); and create a new array '$result' 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 tbm programming I believe it is not the best solution but solved in the case;)

 0
Author: Marcio, 2018-05-19 02:00:06

You can use this:

let vet = ["Italo", "Joao", "Maria"];

let index = vet.indexOf("Italo")

if(index > -1) {
  vet.splice(index,1)
}

The indexOf will search your vector and return 0 if it finds the value, and -1 if it doesn't. Attention that is case sensitive .

Then inside the if the splice removes the element in the passed position.

 -2
Author: italofgcoura, 2020-11-14 23:18:15