looking for some solutions? You are welcome.

SOLVED: PHP: unset from array using if statement

Kiki:

I'm trying to unset specific values using if statement. My code is

$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];

for ($i = 0 ; $i < count($fruits); $i++){ 
 if ($fruits[$i] == 'apple' || $fruits[$i] == 'orange' || $fruits[$i] == 'melon' || $fruits[$i] == 'banana'){
 unset($fruits[$i]);
  }
 }
 print_r($fruits);

I'm expecting it to return

Array
(
  [4] => pineapple
)

But the result is

Array
(
  [3] => banana
  [4] => pineapple
)

Why isn't 'banana' unset from array?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent