ในภาษา PHP หากต้องการวนซ้ำผ่านสมาชิกทุกตัวในอาร์เรย์แบบ Associative Array ควรใช้วิธีใด?
กใช้ foreach loop เช่น foreach($arr as $key => $value)
ขใช้ for loop พร้อม index ตัวเลข เช่น for($i=0; $i<count($arr); $i++)
คใช้ while loop พร้อม next() และ current()
งใช้ do-while loop พร้อม array_pop()
เฉลยอธิบาย
foreach เหมาะที่สุดสำหรับ Associative Array เพราะ Key อาจเป็น String ไม่ใช่ตัวเลข เช่น $arr['name'] จึงไม่สามารถใช้ Index ตัวเลขได้ รูปแบบ foreach($arr as $key => $value) ให้ทั้ง Key และ Value ในแต่ละรอบ ส่วน for loop ใช้ Index ตัวเลขเหมาะกับ Indexed Array, while + next()/current() ทำได้แต่ซับซ้อนกว่า และ do-while + array_pop() จะทำลายอาร์เรย์