如何使用 PHP 根据 arrayOne 中的值过滤 arrayTwo?
How can I filter arrayTwo based on values in arrayOne using PHP?
很抱歉没有使用正确的术语。在编程方面,我非常喜欢 'weekend warrior',但正在努力变得更好。
我有两个以关联数组作为值的索引数组。 ArrayOne 具有 [uid_apps]
的值,我想将其用作 ArrayTwo 的过滤器,因此我可以创建 ArrayThree 。第三个数组将仅包含 ArrayTwo 中与 ArrayOne 中的 [uid_apps]
值匹配的数组项。
我查看了 array_combine() 和 array_intersect(),但没有看到通往成功的清晰路径。我也弄乱了 array_filter(),但无法使其工作。
下面是 arrayOne、arrayTwo 和所需的 arrayThree 的示例。非常感谢您提供的任何帮助。
ArrayOne
[0] => Array
(
[uid_appMembership] => 3
[uid_apps] => 1
[uid_main] => 3
[privileges] => 555
)
[1] => Array
(
[uid_appMembership] => 4
[uid_apps] => 3
[uid_main] => 3
[privileges] => 555
)
ArrayTwo
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 2
[name_apps] => CTE Work Based Learning Solution
[site_apps] => do_000
[team_apps] => do_cte
[admin_uid] => 3
[dir_apps] => do_cte-wbl
)
[2] => Array
(
[uid_apps] => 3
[name_apps] => GHS Parking Permit Solution
[site_apps] => ghs_001
[team_apps] => ghs_parking
[admin_uid] => 3
[dir_apps] => ghs_parking-permits
)
[3] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
需要 ArrayThree 我想使用 arrayOne 和 arrayTwo 来创建这个数组。
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
这是您获得所需数组的方法
$dataa = array();
foreach ($names as $key => $name) {
foreach($ips as $key2=>$ip){
if($name['uid_apps'] == $ip['uid_apps']){
$dataa[] = $name;
}
}
}
print_r($dataa);
遍历数组并检查 uid_apps 值在其他数组中是否可用(如果可用)而不是将其添加到第三个 array.check 中使用数组。
$arr1 = array(
array(
'uid_appMembership' => 3,
'uid_apps' => 1,
'uid_main' => 3,
'privileges' => 555
),
array(
'uid_appMembership' => 4,
'uid_apps' => 3,
'uid_main' => 3,
'privileges' => 555
)
);
$arr2 = array(
array(
'uid_apps' => 1,
'name_apps' => 'GHS Walk Through Evaluation',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_admin',
'admin_uid' => 2,
'dir_apps' => 'ghs_walk-through-evaluation'
),array(
'uid_apps' => 2,
'name_apps' => 'CTE Work Based Learning Solution',
'site_apps' => 'do_000',
'team_apps' => 'do_cte',
'admin_uid' => 3,
'dir_apps' => 'do_cte-wbl'
),array(
'uid_apps' => 3,
'name_apps' => 'GHS Parking Permit Solution' ,
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_parking',
'admin_uid' => 3,
'dir_apps' => 'ghs_parking-permits'
),array(
'uid_apps' => 4,
'name_apps' => 'GHS F-List',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_counseling',
'admin_uid' => 3,
'dir_apps' => 'ghs_flist'
)
);
$arr3 = array();
foreach ($arr2 as $key => $value) {
$res = chk_val($arr1,$value['uid_apps']);
if($res == true){
array_push($arr3,$arr2[$key]);
}
}
function chk_val($arr,$val){
foreach ($arr as $key => $value) {
if(in_array($val,$value)){
return true;
}else{
return false;
}
}
}
很抱歉没有使用正确的术语。在编程方面,我非常喜欢 'weekend warrior',但正在努力变得更好。
我有两个以关联数组作为值的索引数组。 ArrayOne 具有 [uid_apps]
的值,我想将其用作 ArrayTwo 的过滤器,因此我可以创建 ArrayThree 。第三个数组将仅包含 ArrayTwo 中与 ArrayOne 中的 [uid_apps]
值匹配的数组项。
我查看了 array_combine() 和 array_intersect(),但没有看到通往成功的清晰路径。我也弄乱了 array_filter(),但无法使其工作。
下面是 arrayOne、arrayTwo 和所需的 arrayThree 的示例。非常感谢您提供的任何帮助。
ArrayOne
[0] => Array
(
[uid_appMembership] => 3
[uid_apps] => 1
[uid_main] => 3
[privileges] => 555
)
[1] => Array
(
[uid_appMembership] => 4
[uid_apps] => 3
[uid_main] => 3
[privileges] => 555
)
ArrayTwo
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 2
[name_apps] => CTE Work Based Learning Solution
[site_apps] => do_000
[team_apps] => do_cte
[admin_uid] => 3
[dir_apps] => do_cte-wbl
)
[2] => Array
(
[uid_apps] => 3
[name_apps] => GHS Parking Permit Solution
[site_apps] => ghs_001
[team_apps] => ghs_parking
[admin_uid] => 3
[dir_apps] => ghs_parking-permits
)
[3] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
需要 ArrayThree 我想使用 arrayOne 和 arrayTwo 来创建这个数组。
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
这是您获得所需数组的方法
$dataa = array();
foreach ($names as $key => $name) {
foreach($ips as $key2=>$ip){
if($name['uid_apps'] == $ip['uid_apps']){
$dataa[] = $name;
}
}
}
print_r($dataa);
遍历数组并检查 uid_apps 值在其他数组中是否可用(如果可用)而不是将其添加到第三个 array.check 中使用数组。
$arr1 = array(
array(
'uid_appMembership' => 3,
'uid_apps' => 1,
'uid_main' => 3,
'privileges' => 555
),
array(
'uid_appMembership' => 4,
'uid_apps' => 3,
'uid_main' => 3,
'privileges' => 555
)
);
$arr2 = array(
array(
'uid_apps' => 1,
'name_apps' => 'GHS Walk Through Evaluation',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_admin',
'admin_uid' => 2,
'dir_apps' => 'ghs_walk-through-evaluation'
),array(
'uid_apps' => 2,
'name_apps' => 'CTE Work Based Learning Solution',
'site_apps' => 'do_000',
'team_apps' => 'do_cte',
'admin_uid' => 3,
'dir_apps' => 'do_cte-wbl'
),array(
'uid_apps' => 3,
'name_apps' => 'GHS Parking Permit Solution' ,
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_parking',
'admin_uid' => 3,
'dir_apps' => 'ghs_parking-permits'
),array(
'uid_apps' => 4,
'name_apps' => 'GHS F-List',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_counseling',
'admin_uid' => 3,
'dir_apps' => 'ghs_flist'
)
);
$arr3 = array();
foreach ($arr2 as $key => $value) {
$res = chk_val($arr1,$value['uid_apps']);
if($res == true){
array_push($arr3,$arr2[$key]);
}
}
function chk_val($arr,$val){
foreach ($arr as $key => $value) {
if(in_array($val,$value)){
return true;
}else{
return false;
}
}
}