我合并了两个数组来创建以下数组,名为 $ group_wages_array :
Array ( [1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )
我正在尝试测试数组键是否与X匹配,将变量设置为值。这就是我所拥有的:
注意:整个事情都是在while循环中执行的,所以$ thegroup ['group_id']的值会改变。我在这个例子中将它的值设置为“6”。
$thegroup['group_id'] = "6" // This particular group (for simplicity)
if (array_key_exists($thegroup['group_id'], $group_wages_array)) {
$this_wages = // Need this to be 80... how do I do it?
}
那么,如何让$ this_wages等于键值呢?
答案 0 :(得分:8)
您只需使用数组中的密钥即可获取它:
$thegroup['group_id'] = "6" // This particular group (for simplicity)
if (array_key_exists($thegroup['group_id'], $group_wages_array)) {
$this_wages = $group_wages_array[$thegroup['group_id']];
}
此外,数组键不是0,1,2等,因为您在Array ( [1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )
答案 1 :(得分:1)
您正在尝试:
$group_wages_array[6];
和
$thegroup['group_id'] = 6;
您可以将其替换为键。
if (array_key_exists($thegroup['group_id'], $group_wages_array)) {
$this_wages = $group_wages_array[$thegroup['group_id']];
}