从数组中删除元素 - PHP

时间:2016-07-05 09:43:06

标签: php arrays

我已阅读并尝试了很多线程 - 但它对我不起作用。

我有两个数组,想要检查另一个数组。如果$ available_cars_array中列出了汽车,我想将其从$ wanted_cars_array中删除,以便第二次找不到它。

#Create the arrays

 $available_cars_array = array("Volvo", "BMW", "Ford", "Toyota", "Ford", "Jaguar", "Alfa", "Reliant", "Bubble", "Ford");
 $wanted_cars_array = array("Ford", "Vauxhall", "Nissan", "Datsun","Kia", "Hyundai");
 #Get the size of the array
 $arrlength = count($available_cars_array);

 check_if_wanted($available_cars_array, $wanted_cars_array);

    function check_if_wanted($available_cars_array, $wanted_cars_array)
    {
       #Check in for cars in the $available_cars_array

       foreach ($available_cars_array as $available)
        {
                foreach ($wanted_cars_array as $wanted)
                {
                    if (stripos($available, $wanted) !== false)  
                        {

                             unset($wanted_cars_array[$wanted]); #Remove wanted car from wanted cars array
                             echo "Found $wanted </br>";

                        } 
                }   
        } 

    }

6 个答案:

答案 0 :(得分:0)

只需使用array_diff

即可
<?php
    $wanted_cars_array = array_diff($wanted_cars_array, $available_cars_array);

答案 1 :(得分:0)

你试过吗

//returns an array containing the elements from the first array that are not found in the second array
$wantedCars = array_diff($wanted_cars_array, $available_cars_array);
foreach($wantedCars as $wantedCar)
{
echo "Found $wantedCar </br>";
}

我编辑答案的速度太慢@ b0ne首先回答... array_diff是最好的方法,作为本机PHP函数......

答案 2 :(得分:0)

使用array_intersectarray_diff

$available_cars_array = array("Volvo", "BMW", "Ford", "Toyota", "Ford", "Jaguar", "Alfa", "Reliant", "Bubble", "Ford");
$wanted_cars_array = array("Ford", "Vauxhall", "Nissan", "Datsun","Kia", "Hyundai");
// get common elements
$commonElements = array_intersect($available_cars_array,$wanted_cars_array);
// removed matched elements from wanted_cars_array
$new_wanted_cars_array = array_diff($wanted_cars_array, $commonElements); 

答案 3 :(得分:0)

使用此:

$union_cars=array_unique(array_merge($available_cars_array, $wanted_cars_array));  

答案 4 :(得分:0)

如果你想比较这个阵列中的汽车:

 $wanted_cars_array = array("Ford", "Vauxhall", "Nissan", "Datsun","Kia", "Hyundai");

到这个阵列中的汽车:

 $available_cars_array = array("Volvo", "BMW", "Ford", "Toyota", "Ford", "Jaguar", "Alfa", "Reliant", "Bubble", "Ford");

只需使用:

通缉但不可用的汽车

$wanted_but_unavailable_cars_array = array_diff($wanted_cars_array, $available_cars_array);
$wanted_but_unavailable_cars_array = array_values($wanted_but_unavailable_cars_array);

通缉和可用的汽车

$wanted_and_available_cars_array = array_intersect($wanted_cars_array, $available_cars_array);
$wanted_and_available_cars_array = array_values($wanted_and_available_cars_array);

答案 5 :(得分:-1)

试试这个:

$available_cars_array = array("Volvo", "BMW", "Ford", "Toyota", "Ford", "Jaguar", "Alfa", "Reliant", "Bubble", "Ford");
$wanted_cars_array = array("Ford", "Vauxhall", "Nissan", "Datsun","Kia", "Hyundai");

#Get the size of the array
$arrlength = count($available_cars_array);

$wanted_cars_array1 = check_if_wanted($available_cars_array, $wanted_cars_array);
echo '<pre>'; print_r($wanted_cars_array1); exit;

function check_if_wanted($available_cars_array, $wanted_cars_array)
{
    #Check in for cars in the $available_cars_array

    foreach ($available_cars_array as $available)
    {
        foreach ($wanted_cars_array as $key => $wanted)
        {
            if ($available == $wanted)
            {

                echo "Found $wanted </br>";
                unset($wanted_cars_array[$key]); #Remove wanted car from wanted cars array

            }
        }
    }

    return $wanted_cars_array;

}

注意:这是获得所需结果的方法。请参考@Ravi Hirani的解决方案。那个是最好的:)。