php 变量与key匹配,PHP如果array_key_exists,变量等于数组值:如何?

我组合了两个数组来创建以下数组,名为$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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值