迭代php数组中的未知键(使用array_count_values创建)

问题描述 投票:1回答:1

我有这样的数组 -

$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?
}

谢谢

php arrays foreach key
1个回答
1
投票

$key => $value中使用foreach()

foreach ($fruitSorted as $key => $value){
   echo $key . " is the key, and " . $value . " is the value" ;
}
© www.soinside.com 2019 - 2024. All rights reserved.