简体   繁体   English

PHP in_array不会检查所有情况

[英]PHP in_array does not check all cases

I'm removing values from an array in PHP use an in_array check yet for some reason it does not want to check through the entire length of the exclude array . 我正在使用in_array检查从PHP中的数组中删除值,但由于某些原因,它不想检查exclude array的整个长度。 Running this code it removes the Dodge entries from the array but not the Toyota, why is this? 运行此代码会从数组中删除“道奇”条目,但不会从Toyota中删除,这是为什么?

<?php

    $inventory = array(
        '0' => Array
            (
                'car_name' => 'Dodge',
                'set_name' => 'A',
                'edition' => 'B41',
                'location' => 'Houston'
            ),

        '1' => Array
            (
                'car_name' => 'Dodge',
                'set_name' => 'A',
                'edition' => 'B41',
                'location' => 'Houston'
            ),

        '2' => Array
            (
                'car_name' => 'Dodge',
                'set_name' => 'A',
                'edition' => 'B41',
                'location' => 'Houston'
            ),

        '3' => Array
            (
                'car_name' => 'Dodge',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '4' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '5' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '6' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '7' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '8' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '9' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '10' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'
            ),

        '11' => Array
            (
                'car_name' => 'Toyota',
                'set_name' => 'A',
                'edition' => 'VarA31',
                'location' => 'Houston'

            ),
    );

    $exclude = array('Dodge','Toyota');

    for($k=0; $k<sizeof($inventory); $k++)
    {
    if(in_array(trim($inventory[$k]['car_name']), $exclude))
    {   unset($inventory[$k]);}

    }

         $inventory = array_values($inventory);
         echo '<pre>';
         print_r($inventory);
         echo '</pre>';



    ?>

Your problem is that unsetting an element from an enumerated array will adjust the size of your array, but testing using sizeof() to see if you should exit your loop, so each iteration where you unset will reduce the number of values left to test. 您的问题是,从枚举数组中取消设置元素会调整数组的大小,但是使用sizeof()进行测试以查看是否应退出循环,因此未设置的每次迭代都会减少要测试的值的数量。

$exclude = array('Dodge','Toyota');

$size = sizeof($inventory);
for($k=0; $size; $k++) {
    if(in_array(trim($inventory[$k]['car_name']), $exclude)) {
        unset($inventory[$k]);
    }
}

$inventory = array_values($inventory);
echo '<pre>';
print_r($inventory);
echo '</pre>';

You are removing the index:value pair from the array, while iterating it. 您正在迭代时从数组中删除index:value对。 So after unsetting a value the index positions dont match or even exist. 因此,在取消设置值之后,索引位置将不匹配甚至不存在。

Try copying the values you do need, into a new empty array like this: 尝试将所需的值复制到新的空数组中,如下所示:

$exclude = array('Dodge','Toyota');
$filtered = array();

for($k=0; $k<sizeof($inventory); $k++){
    if(in_array(trim($inventory[$k]['car_name']), $exclude)){
        // dont do this because it is changing the count of elements in the array unset($inventory[$k]);
    } else {
        $filtered[] = $inventory[$k]; // push the desired values into a new array
    }
}

echo '<pre>';
print_r($filtered);  // this should have the values that don't have 'Dodge' or 'Toyota'
echo '</pre>';

Edit: Mark's answer would be the best way to go cause it is also better performance wise 编辑:马克的答案将是最好的方法,因为它也是更好的表现明智

use foreach instead for($k=0; $k<sizeof($inventory); $k++) : 使用foreach代替for($k=0; $k<sizeof($inventory); $k++)

foreach($inventory as $k => $row)

this is because while you are unsetting $inventory you skip elements 这是因为在取消设置$inventory会跳过元素

$k = 0, sizeof($inventory) = 12, you unset one of the items, $k++ . $k = 0, sizeof($inventory) = 12,您取消设置其中一项, $k++ $inventory[1] becomes $inventory[0] $inventory[1]变为$inventory[0]

$k = 1, sizeof($inventory) is now 11, you check $inventory[1] but not $inventory[0] (previous known as $inventory[1] ) $k = 1, sizeof($inventory)现在为11,您检查$inventory[1]而不是$inventory[0] (以前称为$inventory[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM