如何检查 php 中的两个数组
How to check two array in php
这里我在结构下面有两个数组
$a = [
"100" => ["name" => "1 A"],
"200" => ["name" => "1 B"],
"300" => ["name" => "1 C"],
"400" => ["name" => "1 D"],
];
$b = [
"user1" => ["100" , "200"],
"user2" => ["100" , "300"],
"user3" => ["100" , "200","400"],
];
现在让我们以 $b
user1 的值为 100 和 200 为例,现在要检查 $a
的值 100 表示名称,200 表示名称?
根据我的数组记录,100 表示值是 "name" => "1 A" & 200 是 "name" => "1 B"
My expected answer
Array
(
[user1] => Array
(
[0] => 1 A
[1] => 1 B
)
[user2] => Array
(
[0] => 1 A
[1] => 1 C
)
[user3] => Array
(
[0] => 1 A
[1] => 1 B
[2] => 1 D
)
)
您可以使用foreach
$r = [];
foreach($activeGroups as $k => $v){
foreach($v as $ik => $iv){
$r[$k][] = $teacherActiveGroupNameData[$iv]['name'];
}
}
array_walk() and array_map() 可能会对您有所帮助。 array_walk()
是对 $activeGroups
数组进行迭代,array_map()
是从 $teacherActiveGroupNameData
数组中提取名称。
array_walk($activeGroups, function (&$val) use ($teacherActiveGroupNameData) {
$val = array_map(function ($val) use ($teacherActiveGroupNameData) {
return $teacherActiveGroupNameData[$val]['name'];
}, $val);
});
echo '<pre>', print_r($activeGroups);
工作demo。
这里我在结构下面有两个数组
$a = [
"100" => ["name" => "1 A"],
"200" => ["name" => "1 B"],
"300" => ["name" => "1 C"],
"400" => ["name" => "1 D"],
];
$b = [
"user1" => ["100" , "200"],
"user2" => ["100" , "300"],
"user3" => ["100" , "200","400"],
];
现在让我们以 $b
user1 的值为 100 和 200 为例,现在要检查 $a
的值 100 表示名称,200 表示名称?
根据我的数组记录,100 表示值是 "name" => "1 A" & 200 是 "name" => "1 B"
My expected answer
Array
(
[user1] => Array
(
[0] => 1 A
[1] => 1 B
)
[user2] => Array
(
[0] => 1 A
[1] => 1 C
)
[user3] => Array
(
[0] => 1 A
[1] => 1 B
[2] => 1 D
)
)
您可以使用foreach
$r = [];
foreach($activeGroups as $k => $v){
foreach($v as $ik => $iv){
$r[$k][] = $teacherActiveGroupNameData[$iv]['name'];
}
}
array_walk() and array_map() 可能会对您有所帮助。 array_walk()
是对 $activeGroups
数组进行迭代,array_map()
是从 $teacherActiveGroupNameData
数组中提取名称。
array_walk($activeGroups, function (&$val) use ($teacherActiveGroupNameData) {
$val = array_map(function ($val) use ($teacherActiveGroupNameData) {
return $teacherActiveGroupNameData[$val]['name'];
}, $val);
});
echo '<pre>', print_r($activeGroups);
工作demo。