Remove values from array without looping in PHP

Tired of the same old looping method to remove values from the array or searching the values in the array and removing them.

This might not be something very new but it can come in handy and might reduce your LOC. I am demonstrating you 2 ways to do so

1. Flip the array and then remove the values.

	$fruits = array(''apple'',''mango'',''banana'',''grapes'',''orange'');
	$fruits = array_flip($fruits);
	if(isset($fruits[''banana'']))
		unset($fruits[''banana'']);
        $fruits = array_flip($fruits);

Firstly your array would look something like this:

Array ( [0] => apple [1] => mango [2] => banana [3] => grapes [4] => orange )

When you flip the array, it will make the values of the array as keys and keys as values, so with the first flip you will get:

Array ( [apple] => 0 [mango] => 1 [banana] => 2 [grapes] => 3 [orange] => 4 )

Now you can check if the key exists in the array using isset and then remove the key using unset as shown in the example, so the resultant array would be

Array ( [apple] => 0 [mango] => 1 [grapes] => 3 [orange] => 4 )

Now you can get the array to its original form by flipping it again using array_flip, so it will remove the value and the array would be,

Array ( [0] => apple [1] => mango [3] => grapes [4] => orange )

Even this would be a long process if you want to remove multiple values from the array, in that case you can try something even more better than the previous example, demonstrating it in my second example

2. Finding the new array using difference, array_diff() function compares array and returns the values that are different

	$fruits = array(''apple'',''mango'',''banana'',''grapes'',''orange'');
	$removeValues = array(''mango'',''grapes'');
        $fruits = array_diff($fruits,$removeValues);

Here $fruits contains the original array, $removeValues contains the values to be removed from array $fruits.
After using array_diff the resultant array $fruits will be as below:

Array ( [0] => apple [2] => banana [4] => orange )

Hope this helps you to code better with less LOC.