如果键是字符串而不是int,我该如何制作一个循环来打印数组中的所有元素


How do I make a loop that will print out all of the elements in my array if the keys are strings and not ints?

我想打印出数组中的所有值,但是元素的键是字符串。如果键是字符串而不是int,我如何制作一个循环来打印数组中的所有元素?

这是我迄今为止的代码:http://codepad.viper-7.com/xGhmhX

您可以使用foreach构造:

foreach ($your_array as $key => $value) {
    // Work with either $key or $value
}

或者,如果你不需要循环中的密钥,你可以使用:

foreach ($your_array as $value) {
    // Work with $value
}