如何在php上获取字符串数组的索引?

1 php arrays string indexing numbers

可以说我有一个多维字符串数组:

.food = array(
            'vegetable' => array(
                               'carrot' => 'blablue', 
                               'potato' => 'bluebla', 
                               'cauliflower' => 'blabla'
                           ), 
            'Fruit' => array(
                               'apple' => 'chicken65', 
                               'orange' => 'salsafood', 
                               'pear' => 'lokkulakka'
            )
);
Run Code Online (Sandbox Code Playgroud)

是否可以使用索引作为数字来访问数组,而不是使用键的名称?因此,为了访问chicken65,我将输入echo $ food [1] [0]; 我不想使用数字作为键,因为它是一个大数组,如果我使用字符串作为键,它会更加用户友好,它会让我在高级别上进行for循环.

Bra*_*rad 6

你可以做foreach循环来实现与典型的for循环相同的东西.

foreach ($foods as $key => $value) {
    //For the first iteration, $key will equal 'vegetable' and $value will be the array
}
Run Code Online (Sandbox Code Playgroud)