【发布时间】:2011-12-06 12:13:08
【问题描述】:
我将两个数组组合起来创建了以下数组,命名为 $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']转换为 int。 -
您可以使用任何您想要的作为数组键,只要 PHP 可以将其类型转换为字符串或整数。这包括将一个数组引用嵌入到另一个数组引用中:
$outer[$inner[1]],所以是$group_wages_array[$thegroup['group_id']]。 -
+1 详细了解您的变量是什么以及如何使用它们。我希望更多的人会这样做,而不是仅仅在没有足够上下文的情况下转储代码。