我有这样的数组 -
$fruit = Array ( [0] => bananas [1] => apples [2] => apples [3] => oranges [4] => oranges [5] => apples )
我使用array_count_values
创建了一个新数组来计算单个水果。
$fruitSorted = array_count_values($fruit)
看起来像这样:
Array ( [bananas] => 1 [oranges] => 2 [apples] => 3
到目前为止,我很高兴。但我想迭代第二个数组,但不知道键的名称。它应该是一个多维数组吗?我不知道如何直接转换它。更直接,我想
foreach ($fruitSorted as $a){
echo $a." is the value";
//how to I select the key instead without knowing its name?
}
谢谢
在$key => $value
中使用foreach()
:
foreach ($fruitSorted as $key => $value){
echo $key . " is the key, and " . $value . " is the value" ;
}