我在另一个数组中有一个数组 - 这是我的print_r:
Array
(
[0] => Array
(
[ProductID] => 180
[ProductName] => Male Thread BSPT Coupling
[ProductImage] => 054-male-thread-bspt-coupling.jpg
[Termination] => 1/4\"
[Length] => 58
[Diameter] => 23
[Part] => 054CVMT14SNN
[Price] => 3.26
[quantity] => 22
)
[1] => Array
(
[ProductID] => 183
[ProductName] => Male Thread BSPT Plug
[ProductImage] => 054-male-thread-bspt-plug.jpg
[Termination] => 1/4\"
[Length] => 38
[Diameter] =>
[Part] => 054PNMT14SXN
[Price] => 0.67
[quantity] => 22
)
)
我需要从Part = X
的数组中删除答案 0 :(得分:0)
试试这个(demo):
$arr = array(
'a' => array('h' => 10, 'w' => 40, 'd' => 100, 'part' => 'A10'),
'b' => array('h' => 20, 'w' => 30, 'd' => 400, 'part' => 'A20'),
'c' => array('h' => 30, 'w' => 20, 'd' => 900, 'part' => 'A30'),
'd' => array('h' => 40, 'w' => 10, 'd' => 1600, 'part' => 'A40'),
);
$part = 'A10';
$res = array_filter($arr, function ($elem) use ($part) {
return $elem['part'] != $part;
});
print_r($res);
$res
将是输入数组,不包括part
元素设置为A10
的任何项目。
答案 1 :(得分:-1)
试试这个(请注意,这只会删除第一次出现):
if(($key = array_search($delPartWithThisId, $partsArray)) !== false) {
unset($partsArray[$key]);
}
或者:
foreach (array_keys($partsArray, $delPartWithThisId, true) as $key) {
unset($partsArray[$key]);
}
在第二个解决方案中,变量$delPartWithThisId
可以是[1,2,3]
此外,我建议使用array_values()
重新组织数组索引