我组合了两个数组来创建以下数组,名为$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等于键值呢?
解决方法:
您只需使用数组中的键来获取它:
$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等,因为您在数组中明确设置它们([1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )
标签:php,arrays,variables
来源: https://codeday.me/bug/20190723/1514853.html