Reputation: 333
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 the array?
Upvotes: 8
Views: 2589
Reputation: 11
use This:
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$fruit_num = count($fruits);
for ($i = 0 ; $i < $fruit_num; $i++){
if ($fruits[$i] == 'apple' || $fruits[$i] == 'orange' || $fruits[$i] == 'melon' || $fruits[$i] == 'banana'){
unset($fruits[$i]);
}
}
print_r($fruits);
instead of :
$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);
Output :
Array ( [4] => pineapple )
Upvotes: 1
Reputation: 11642
Because after removing the first 3 element the $i
value is 3 but the count of the array is 2 so the for loop breaks - remember the loop condition is re-evaluating in each iteration. Change to this will give you the desire output:
$size = count($fruits)
for ($i = 0 ; $i < $size; $i++){
Generally, using unset
with for
loop with int index is not recommended
Upvotes: 11
Reputation: 84
Problem is here count($fruits), because every loop cycle array going small.
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$count = count($fruits);
for ($i = 0; $i < $count; $i++){
if ($fruits[$i] == 'apple' || $fruits[$i] == 'orange' || $fruits[$i] == 'melon' || $fruits[$i] == 'banana'){
unset($fruits[$i]);
}
}
print_r($fruits);
Hope this will help!
Upvotes: 1
Reputation: 26460
There are many ways to Rome, as they say..
foreach()
A cleaner approach would be to use foreach
instead of for
loops, and checking against an array using in_array()
instead of individually checking all the fruits.
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
foreach ($fruits as $key=>$fruit) {
if (in_array($fruit, ['apple', 'orange', 'melon', 'banana'])) {
unset($fruits[$key]);
}
}
print_r($fruits);
array_filter()
A more "fancy" way would be a one-liner using array_filter()
,
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$fruits = array_filter($fruits, function($fruit) {
return !in_array($fruit, ['apple', 'orange', 'melon', 'banana']);
});
print_r($fruits);
array_diff()
Even simpler, use array_diff()
, which finds all elements that exists in only one of the arrays (essentially removing the duplicates).
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$remove = ['apple', 'orange', 'melon', 'banana'];
$result = array_diff($fruits, $remove);
array_intersect()
There's also array_intersect
, which is sort of the inverse of array_diff()
. This would find elements that exists in both arrays, and return that.
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$find = ['pineapple'];
$result = array_intersect($fruits, $find);
foreach()
array_filter()
array_diff()
array_intersect()
in_array()
array_diff()
array_filter()
array_intersect()
foreach()
Upvotes: 16
Reputation: 581
I would recommend you to use foreach
instead of for loop
. Try the following code below.
<?php
$fruitss = ['apple', 'orange', 'melon', 'banana', 'pineapple','h'];
foreach ($fruitss as $key=>$fruits) {
//echo $fruits;
if ($fruits == 'apple' || $fruits == 'orange' || $fruits == 'melon' || $fruits == 'banana') {
unset($fruitss[$key]);
}
}
print_r($fruitss);
?>
Upvotes: 4
Reputation: 23958
There are many possible ways to do this in PHP as PHP is very rich with array and string functions.
In this answer, two approaches are discussed.
Basically, you want to remove the elements from array.
So, take another array with the elements you want to remove.
Find out the difference between your array and the new array.
Difference will definitely your expected result.
You can use array_diff()
Beauty of this approach is:
You can do it minimal lines of code, also, no loops required.
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$remove = ['apple', 'orange', 'melon', 'banana'];
$test = array_diff($fruits, $remove);
echo '<pre>';print_r($test);echo '</pre>';
Output:
Array
(
[4] => pineapple
)
Or even using array_interset()
Take a temporary array with the only element(s) you want to maintain in your array.
Get array intersecting element.
You will have all other elements removed.
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$two = ['pineapple'];
$test2 = array_intersect($fruits, $two);
echo '<pre>';print_r($test2);echo '</pre>';
Output:
Array
(
[4] => pineapple
)
Upvotes: 2
Reputation: 2005
I think problem is $i<count($fruits)
. It reduce the array size. So it's runs only 3 times. SO assign the array size to variable first.
<?php
$fruits = ['apple', 'orange', 'melon', 'banana', 'pineapple'];
$n=count($fruits);
for ($i = 0 ; $i <$n ; $i++){
if ($fruits[$i] == 'apple' || $fruits[$i] == 'orange' || $fruits[$i] == 'melon' || $fruits[$i] == 'banana'){
unset($fruits[$i]);
}
}
print_r($fruits);
?>
Upvotes: 4