-2

For instance:

$array = ["a", "b", "c", "a", "a"];

I want to remove all "a" elements to get output:

$array = ["b", "c"];

Should also work for ints and floats.Thanks guys!

7
  • 1
    array_filter() function Commented May 26, 2016 at 13:02
  • 1
    Show us what you have tried? - And shortest way possible? As in speed or amount of code? Commented May 26, 2016 at 13:03
  • Can you give an example? Commented May 26, 2016 at 13:03
  • Shortest way in terms of amount of code. Commented May 26, 2016 at 13:06
  • 2
    dup of stackoverflow.com/questions/7225070/… Commented May 26, 2016 at 13:23

2 Answers 2

0

Use array_diff as defined below. You need make removed value as array and use simply the array diff to get the desired result.

$array = ["a", "b", "c", "a", "a"];

$arr = array_diff($array, array("a"));

print_r($arr); //Array ( [1] => b [2] => c )
Sign up to request clarification or add additional context in comments.

Comments

0

Try this, It will work for ints, floats and strings..

$array = [1, "b", "c", 1, "a"];

foreach (array_keys($array,1) as $key) ////array_key return matched keys
{
   unset($array[$key]); ///remove value with key
}

print_r($array);

This will give you :

Array
(
    [1] => b
    [2] => c
    [4] => a
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.