Rafeeq Mohamed
Rafeeq Mohamed

Reputation: 190

how to remove an array from multi dimensional array if include specific item in php

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

Upvotes: 2

Views: 115

Answers (7)

sevavietl
sevavietl

Reputation: 3792

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.

Upvotes: 1

BERNARDO
BERNARDO

Reputation: 219

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.

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34924

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
        )

)

Upvotes: 0

J. Quick
J. Quick

Reputation: 303

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");
?>

Upvotes: 1

Ataur Rahman
Ataur Rahman

Reputation: 1791

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

Upvotes: 3

iainn
iainn

Reputation: 17434

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

Upvotes: 5

Ayaz Ali Shah
Ayaz Ali Shah

Reputation: 3541

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

Upvotes: 0

Related Questions