Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: unset from array using if statement

Tags:

arrays

php

unset

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?

like image 385
Kiki Avatar asked Mar 13 '19 06:03

Kiki


People also ask

How do you unset an array?

Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array.

What is unset () in PHP?

unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.

How do you unset multiple values in PHP?

You have to use for loop for this. you can use foreach loop but it will not unset all variable one variable still remains.

How do you empty an array in PHP?

Syntax to create an empty array:$emptyArray = []; $emptyArray = array(); $emptyArray = (array) null; While push an element to the array it can use $emptyArray[] = “first”. At this time, $emptyArray contains “first”, with this command and sending “first” to the array which is declared empty at starting.


1 Answers

There are many ways to Rome, as they say..

Using 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);

Using 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);

Using 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);

Using 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); 
  • Live demo using foreach()
  • Live demo using array_filter()
  • Live demo using array_diff()
  • Live demo using array_intersect()
  • PHP.net on in_array()
  • PHP.net on array_diff()
  • PHP.net on array_filter()
  • PHP.net on array_intersect()
  • PHP.net on foreach()
like image 199
Qirel Avatar answered Oct 03 '22 19:10

Qirel