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

// our initial array
$arr_main = array('blue', 'green', 'red', 'yellow', 'green', 'orange', 'yellow', 'indigo', 'red');

// remove the elements who's values are yellow or red
$arr_to_rem = array('red', 'yellow');

echo '
';
print_r($arr_main);
echo '

‘;

$arr_main = array_diff($arr_main, $arr_to_rem);

echo ‘

';
print_r($arr_main);
echo '

‘;

$arr_main = array_values($arr_main);
echo ‘

';
print_r($arr_main);
echo '

‘;

‘;

This is the output from the code above:

Array
(
    [0] => blue
    [1] => green
    [2] => red
    [3] => yellow
    [4] => green
    [5] => orange
    [6] => yellow
    [7] => indigo
    [8] => red
)
Array
(
    [0] => blue
    [1] => green
    [4] => green
    [5] => orange
    [7] => indigo
)
Array
(
    [0] => blue
    [1] => green
    [2] => green
    [3] => orange
    [4] => indigo
)


Enjoy!

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

5 × 2 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like

Pico – Flat-File CMS

Pico is an open source CMS application that is capable of very few things. It uses flat-files as the database and built with PHP. Simply, there is no setup and the app works instantly.

Calculate folder and subfolders size with PHP

How to read the size of a directory using PHP? Here is a simple function which could help read the size of the directory, number of directories and the number of files in the given directory.

Fast, Effective PHP Compression With .htaccess

PHP compression is an excellent method of conserving bandwidth and reducing client download times. We have already discussed an excellent method for CSS compression, and in this article we share a super-easy technique for compressing all PHP content without editing a single file.