PHP使用另一个数组删除数组中的数组
我有两个数组。第一个是我的数组,第二个是API响应:PHP使用另一个数组删除数组中的数组,php,arrays,Php,Arrays,我有两个数组。第一个是我的数组,第二个是API响应: $array1 = [ ["id" => 45, "name" => "toto"], ["id" => 50, "name" => "tata"], ["id" => 31, "name" => "titi"], ["id" => 82, "name" => "tutu"], ["id" => 12, "name" => "tototo"]
$array1 = [
["id" => 45, "name" => "toto"],
["id" => 50, "name" => "tata"],
["id" => 31, "name" => "titi"],
["id" => 82, "name" => "tutu"],
["id" => 12, "name" => "tototo"]
];
$array2 = [
"45" => ["status" => false],
"50" => ["status" => true],
"31" => ["status" => true],
"82" => ["status" => false],
"12" => ["status" => true]
];
我想在array1
中删除array2
中状态为false
的所有id
,或者保留状态为true的所有id
在本例中,我需要得到:
$array1 = [
["id" => 50, "name" => "tata"],
["id" => 31, "name" => "titi"],
["id" => 12, "name" => "tototo"]
];
因为id
45
和82
在第二个数组中为false,所以我将它们从第一个数组中删除
我如何在不使用多个循环的情况下做到这一点?如果我们使用php函数,如array\u diff
或类似的函数,有一个解决方案。您可以使用array\u filter
来过滤$array1
。如果id
存在且状态为true,则返回true
$array1 = ...
$array2 = ...
$result = array_filter($array1, function( $o ) use( $array2 ) {
return isset( $array2[ $o["id"] ] ) && $array2[ $o["id"] ]["status"];
});
这将导致:
Array
(
[1] => Array
(
[id] => 50
[name] => tata
)
[2] => Array
(
[id] => 31
[name] => titi
)
[4] => Array
(
[id] => 12
[name] => tototo
)
)
$array1 = [
["id" => 45, "name" => "toto"],
["id" => 50, "name" => "tata"],
["id" => 31, "name" => "titi"],
["id" => 82, "name" => "tutu"],
["id" => 12, "name" => "tototo"]
];
$array2 = [
"45" => ["status" => false],
"50" => ["status" => true],
"31" => ["status" => true],
"82" => ["status" => false],
"12" => ["status" => true]
];
foreach ($array1 as $tocheck) {
if ($array2[$tocheck['id']]['status']) {
$new[] = $tocheck;
}
}
print_r($new);
最具可读性的解决方案通常比使用奇特的数组*
函数要好,即在这种情况下,一个简单的foreach
循环就足够了:
请尝试此代码
<?php
$array1 = [
["id" => 45, "name" => "toto"],
["id" => 50, "name" => "tata"],
["id" => 31, "name" => "titi"],
["id" => 82, "name" => "tutu"],
["id" => 12, "name" => "tototo"]
];
$array2 = [
"45" => ["status" => false],
"50" => ["status" => true],
"31" => ["status" => true],
"82" => ["status" => false],
"12" => ["status" => true]
];
foreach($array1 AS $key => $value){
if(!$array2[$value['id']]['status']){
unset($array1[$key]);
}
}
echo "<pre>";
print_r($array1);
?>
您可以使用array\u walk
// Make $array1 associative.
$array1 = array_column($array1, null, "id");
// Find all true values in $array1 from $array2
$result = array_intersect_key($array1, array_intersect(array_combine(array_keys($array2), array_column($array2, "status")), [true]));
这可能是可读性最低的解决方案:-)
Array\u intersect和Array\u column也可以完成此任务
我在id上使两个数组都关联,然后使用array_intersect_键获得真值
array_walk($array1, function(&$item,$key) use(&$array1, $array2){
if(!$array2[$item['id']]['status']) // checking status if false
unset($array1[array_search($item['id'], array_column($array1,'id'))]); // then unset
});
print_r($array1);
对于同样的问题,这里还有一个解决方案
-将用户提供的函数应用于阵列的每个成员
-在数组中搜索给定值,如果成功,则返回第一个对应的键
-从输入数组中的单个列返回值
正在工作。这是更合适的响应为什么不使用unset而不是创建新数组?如果我们创造出更好的性能?@johnhi,你可以在这里阅读易变性/不变性原则;DR您希望防止代码中的副作用,因此您尝试避免对现有数据进行变异。
$res=[];
array_walk($array1, function(&$v,$k) use (&$res,$array2){
($array2[$v['id']]['status'] == 1) ? ($res[] = $v): '';
});
// Make $array1 associative.
$array1 = array_column($array1, null, "id");
// Find all true values in $array1 from $array2
$result = array_intersect_key($array1, array_intersect(array_combine(array_keys($array2), array_column($array2, "status")), [true]));
array_walk($array1, function(&$item,$key) use(&$array1, $array2){
if(!$array2[$item['id']]['status']) // checking status if false
unset($array1[array_search($item['id'], array_column($array1,'id'))]); // then unset
});
print_r($array1);