我有2个数组。第一个是我的数组,第二个是API响应:
$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
中删除id
中状态为array2
的所有false
,或将所有id
保持状态为{{1 }}
在此示例中,我需要获取:
true
由于$array1 = [
["id" => 50, "name" => "tata"],
["id" => 31, "name" => "titi"],
["id" => 12, "name" => "tototo"]
];
id
和45
在第二个数组中为假,因此我将它们从第一个数组中删除。
如何在不使用多个循环的情况下做到这一点?如果我们使用诸如82
之类的php函数或类似的方法,有解决方案?
答案 0 :(得分:4)
您可以使用import imp
some_module = imp.load_source('some_module', '/path/to/some_module.py')
来过滤array_filter
。如果$array1
存在并且状态为true,则返回true。
id
这将导致:
$array1 = ...
$array2 = ...
$result = array_filter($array1, function( $o ) use( $array2 ) {
return isset( $array2[ $o["id"] ] ) && $array2[ $o["id"] ]["status"];
});
答案 1 :(得分:1)
最具可读性的解决方案通常比使用精美的array_*
函数要好,也就是说,在这种情况下,简单的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]
];
$result = [];
foreach($array1 as $arrayEl) {
$id = $arrayEl['id'];
if($array2[$id]['status'] === true) {
$result[] = $arrayEl;
}
}
var_dump($result);
注意:我们不是在修改原始内容,而是创建一个新的结果数组。您可能要根据阵列包含/可能不包含的键添加其他isset
检查。
答案 2 :(得分:1)
请尝试使用此代码。
<?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);
?>
从第一个数组中找到键,并使用true或false值将其取消设置。
输出:
Array
(
[1] => Array
(
[id] => 50
[name] => tata
)
[2] => Array
(
[id] => 31
[name] => titi
)
[4] => Array
(
[id] => 12
[name] => tototo
)
)
答案 3 :(得分:1)
您可以使用array_walk
$res=[];
array_walk($array1, function(&$v,$k) use (&$res,$array2){
($array2[$v['id']]['status'] == 1) ? ($res[] = $v): '';
});
答案 4 :(得分:1)
这里还有一个解决方案,
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);
array_walk —将用户提供的函数应用于数组的每个成员
array_search —在数组中搜索给定值,如果成功,则返回第一个对应的键
array_column —从输入数组的单个列中返回值
工作demo。
答案 5 :(得分:0)
这可能是可读性最低的解决方案:-)
Array_intersect和array_column也可以完成工作。
我将两个数组都关联为id,然后使用array_intersect_key获得真实值。
InternetDomainName.from("test.blogspot.com").topDomainUnderRegistrySuffix() -> blogspot.com
答案 6 :(得分:-1)
$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);