我有两个数组,我想匹配第二个数组
['Name']
值的[Seltemgr]
值,如果['Name']=>value == [Seltemgr]=>value
则<input type="checkbox" checked="checked">
,否则未选中,是否可以匹配具有不同键的不同元素数量的两个多维数组。
Array
(
[0] => Array
(
[tnid] => 45
[Name] => Financial Tips
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/5
)
[1] => Array
(
[tnid] => 42
[Name] => Products
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/2
)
[2] => Array
(
[tnid] => 44
[Name] => Health Tips
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/4
)
[3] => Array
(
[tnid] => 43
[Name] => Personal Events
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/3
)
[4] => Array
(
[tnid] => 41
[Name] => Calendar
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/1
)
)
Array
(
[0] => Array
(
[emarketid] => 77
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Calendar
[seltemname] =>
)
[1] => Array
(
[emarketid] => 78
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Financial Tips
[seltemname] =>
)
[2] => Array
(
[emarketid] => 79
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Merry Christmas
[seltemname] =>
)
[3] => Array
(
[emarketid] => 80
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Drip financial tip 3
[seltemname] =>
)
)
$match = [];
foreach ($array1 as $k1=>$a1){
foreach($array2 as $k2=>$a2){
if($a2['Seltemgr'] == $a1['Name']){
$match[] = array($k1,$k2);
}
}
}
因此 $match 包含 Name 和 Seltemgr 匹配的每个数组的索引。并不是说这是执行此操作的最佳方法,但相交函数通常需要键来匹配。
我可能建议将
found
元素附加到第一个数组的每一行。 然后,您可以迭代该数组来根据需要打印标记并设计其样式。 一旦在进行迭代值检查时遇到最佳性能的真实评估,array_any()
调用就会短路。 演示
var_export(
array_map(
fn($row) => $row
+ ['found' => array_any($selected, fn($selRow) => $row['Name'] == $selRow['Seltemgr'])],
$checkboxes
)
);