2

Before:

$cars = array
  (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );

i want remove whole array witch include "BMW" as an item

result like this:

$cars = array
  (
  array("Volvo",22,18),
  array("Saab",5,2),
  array("Land Rover",17,15)
  );
0

7 Answers 7

5

You can use array_filter to remove elements from an array based on custom logic. The callback function should return true or false if the element should be kept or removed respectively.

$filter = 'BMW';

$cars = array_filter($cars, function($car) use ($filter) {
    return $car[0] !== $filter;
});

Here, we're filtering out all rows where the first element is equal to the $filter variable.

See https://eval.in/950579

Sign up to request clarification or add additional context in comments.

6 Comments

Will not work if array is like [15,13,"BMW"],, check your demo
@user2486, yeah, in that case, in_array() comes handy..... stackoverflow.com/a/48641534/5527461
I was working against the examples given in the question, but I appreciate that the wording doesn't match that. Ataur Rahman's answer works best if the arrays can be in any order.
yes just wanted to mention that, isset is more faster than in_array
How does isset help when searching an array?
|
3

Just to extend @iainn answer

'BMW' may not always be the first element, so... in_array()

$filter = 'BMW';

$filteredCars = array_filter($cars, function($car) use($filter){
                       return in_array($filter, $car) === false;
                });

4 Comments

Good , in_array would work, just wanted to mention that, isset is more faster than in_array.
What's the benefit of 'faster' if there's a possibility of not working ?
yes definitely, but here isset will also work. see my answer
I am not saying your answer is wrong. even I upvoted your answer. Just wanted to mention about this concept
1

You can do:

<?php

$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

function removeFromArray($array, $make){
    $i = 0;
    $out = array();
    foreach ($array as $element) {
        if($element[0] != $make){
            $out[$i] = $element;
            $i++;
        }
    }
    return $out;
}
$cars = removeFromArray($cars, "BMW");
?>

Comments

1

I did this little function, keep in mind that the car-make has to be key [0].

Example

<?php
$cars = array
(
    array("Volvo", 22, 18),
    array("BMW", 15, 13),
    array("Saab", 5, 2),
    array("Land Rover", 17, 15)
);

function removeCar (array $cars, string $name): array
{
    $returnArray = array();
    foreach ($cars as $array => $car) {
        if ($car[0] != $name) {
            $returnArray[] = $car;
        }
    }
    return $returnArray;
}

$cars = removeCar($cars, "BMW");
print_r($cars);

Output

Array ( [0] => Array ( [0] => Volvo [1] => 22 [2] => 18 ) [1] => Array ( [0] => Saab [1] => 5 [2] => 2 ) [2] => Array ( [0] => Land Rover [1] => 17 [2] => 15 ) )

Hope this helps. Regards.

Comments

1

You can use array_intersect_key and array_column:

// Exclude one car.
$exclude = 'BMW';
$result = array_intersect_key($cars, array_filter(
    array_column($cars, 0),
    function ($car) use ($exclude) {
        return $car !== $exclude;
    }
));

var_dump($result);

// Exclude multiple cars.
$exclude = ['BMW', 'Saab'];
$result = array_intersect_key($cars, array_diff(array_column($cars, 0), $exclude));

var_dump($result);

Here is the demo.

Comments

0

You can check each array using foreach() loop.

   $cars = array
      (
      array("Volvo",22,18),
      array("BMW",15,13),
      array("Saab",5,2),
      array("Land Rover",17,15)
      );


    $cars_filtered = array();  
    foreach($cars as $single){
        if($single[0]!='BMW'){
            $cars_filtered[] = $single;
        }   
    }

    print_r($cars_filtered);

Comments

0

To exclude item exist in array. You can do it by array_flip and isset

foreach($cars as $key=>$value)  {
  if(isset(array_flip($value)["BMW"])){
    unset($cars[$key]);
  }
}

Live demo : https://eval.in/950611

Array
(
    [0] => Array
        (
            [0] => Volvo
            [1] => 22
            [2] => 18
        )

    [2] => Array
        (
            [0] => Saab
            [1] => 5
            [2] => 2
        )

    [3] => Array
        (
            [0] => Land Rover
            [1] => 17
            [2] => 15
        )

)

10 Comments

I am not much of a picky person.. but calling array_flip in foreach ??? seriously? What if the array consists of thousand elements?
@AtaurRahman : then even it is more faster : check this
@AtaurRahman : Also check for this, isset vs in_array
Bro, You don't have to prove fastness of those function to me. I never said in_array is faster. But there is always some situation based implementation
Okay. Then please do some benchmark rather than referencing five years old posts.... Create an array of 1000 elements (each on is an array of more 10 element)similar to OP's example and then benchmark how both of our codes perform. I don't want to be an arrogant nerd, but you should check out your own code first (Like I did).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.