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)
  );
Rafeeq Mohamed
  • 182
  • 1
  • 4
  • 14

7 Answers7

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

iainn
  • 16,826
  • 9
  • 33
  • 40
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;
                });
Ataur Rahman
  • 1,671
  • 14
  • 12
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.

BERNARDO
  • 219
  • 2
  • 12
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");
?>
J. Quick
  • 303
  • 1
  • 7
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.

sevavietl
  • 3,762
  • 1
  • 14
  • 21
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);
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
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
        )

)
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • 1
    I am not much of a picky person.. but calling `array_flip` in `foreach` ??? seriously? What if the array consists of thousand elements? – Ataur Rahman Feb 06 '18 at 11:22
  • @AtaurRahman : then even it is more faster : [check this](http://php.net/manual/en/function.array-flip.php#106895) – Niklesh Raut Feb 06 '18 at 11:25
  • @AtaurRahman : Also check for this, [isset vs in_array](https://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset) – Niklesh Raut Feb 06 '18 at 11:26
  • 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` – Ataur Rahman Feb 06 '18 at 11:27
  • ok great, but we can control the situation, we are programmers. – Niklesh Raut Feb 06 '18 at 11:29
  • 1
    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). – Ataur Rahman Feb 06 '18 at 11:35
  • @AtaurRahman : Both works as same , I didn't find any difference.Thanks – Niklesh Raut Feb 06 '18 at 12:16
  • @AtaurRahman: I never wanted to share something like this but yeah it matters : https://eval.in/950765, but yes it `situation based implementation` – Niklesh Raut Feb 06 '18 at 12:26
  • LOL... It isn't about the cycle of loop, its about size and content of the array... Let me know what will be the result and memory footprint of the array if I flip `array('id => 1, description => 'blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahbl')` – Ataur Rahman Feb 06 '18 at 12:31
  • And You manipulated the benchmark as you stored flipped array in a variable outside of loop while did opposite to `in_array()`, I lost my interest in this topic. I won't reply anymore. Good Bye. – Ataur Rahman Feb 06 '18 at 12:38