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. Running this code it removes the Dodge entries from the array but not the Toyota, why is this?
<?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>';
?>
forloops are not often used in languages supporting some kind offoreachconstruct. If you did this viaforeach ($inventory as $k => $v)you'll find it much easier to manage.forloop. If you have to "do a thing to each element in a collection", choose aforeachinstead.