Remove only one value from the array

0

I have an array like this

$arr = array('10', '12', '10', '15', '18', '18', '7', '18', '18', '15');

Four times the value 18. I need to extract only one value, example one 18, and it goes like this:

$arr = array('10', '12', '10', '15', '18', '7', '18', '18', '15');

And with any other number up to zero and no more number, example 18, there is no limit on numbers.

As if it were a shopping cart logic

I have 10 products number 18, if I remove one at a time, the number 18 goes out of the array

I hope it has become clearer now

    
asked by anonymous 06.07.2017 / 00:03

1 answer

1

For this we find the position with the function array_search :

$posicao = array_search('18',$arr);

And then delete this position with the function unset :

unset($arr[$posicao]);

Or doing it all together:

unset($arr[array_search('18',$arr)]);

Example

    
06.07.2017 / 00:25