Category Archives: arrays

PHP: Remove Values from Array

If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PLAIN TEXT
PHP:

// our initial array

$arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");

print_r($arr);

 

// remove the elements who's values are yellow or red

$arr = array_diff($arr, array("yellow", "red"));

print_r($arr);

 

// optionally you could reindex [...]