How to iterate over an array of arrays

后端 未结 3 1972
不思量自难忘°
不思量自难忘° 2021-02-18 20:32

What\'s the best way to iterate over an array of arrays?

sounds = [ [Name_1, link_1], [Name_2, link_2], [Name_3, link_3], [Name_4, link_4] ]

I

3条回答
  •  余生分开走
    2021-02-18 20:58

    Use a nested loop. The outer one iterates over sounds, while the inner one iterates over the current element from sounds.

    Of course, in that particular example, it would probably be easiest just to directly reference the elements of the inner arrays. That way, you could just print

  • $inner[0], $inner[1]
  • (note that I've never used Ruby, so I don't know how arrays are indexed, let alone printing syntax).

提交回复
热议问题