PHP如果array_key_exists,变量等于数组值:如何?

Ose*_*eer 3 php arrays variables

我组合了两个数组来创建以下数组,名为$ group_wages_array:

Array ( [1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )
Run Code Online (Sandbox Code Playgroud)

我试图测试数组键是否匹配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?

}
Run Code Online (Sandbox Code Playgroud)

那么,如何让$ this_wages等于键值呢?

swa*_*ins 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']];
}
Run Code Online (Sandbox Code Playgroud)

此外,数组键不是0,1,2等,因为您明确设置它们 Array ( [1] => 500 [4] => 44 [6] => 80 [3] => 11.25 )