在PHP中分别从数组中调用数组键和值

时间:2015-01-12 03:48:46

标签: php arrays

我不想使用任何foreachwhile循环。我有一个如下数组:

$q5_value = array( 
    "Essentialto our Business " => "$avg_5_1",
    "promising but not  yet optimized" => "$avg_5_2" , 
    "Somewhat effectiv" => "$avg_5_3",
    "we're curious, but haven't tried these yet" => "$avg_5_4",
    "Not very effective" => "$avg_5_5",
    "We still have alot to figure out about this" => "$avg_5_6",                 
    "Not worth the effort" => "$avg_5_7"
); 

rsort($q5_value);

现在我打电话

echo $q5_value[0];

这显示了变量之间的最大整数。我也希望从左侧显示字符串。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

问题在于" rsort"。 rsort将在排序过程中删除密钥。你应该使用" arsort"保留现有密钥。

假设您的[0]数组元素已正确排序,您可以使用key()函数获取密钥。



arsort($q5_value);
print key($q5_value). " | ". $q5_value[0];