You need to execute a block of code in your PHP script for each key-value pair in an associative array. How would you do this using a foreach loop?

  • Use the "for" loop and access the elements using their indexes
  • Use the "foreach" loop and access the elements using the "key" and "value" variables
  • Use the "while" loop and access the elements using the "current" and "next" functions
  • Use the "foreach" loop and access the elements using the "key" and "value" pairs
The correct option is: "Use the 'foreach' loop and access the elements using the 'key' and 'value' variables." In PHP, you can use the foreach loop with an associative array to iterate over each key-value pair. During each iteration, you can access the key of the current element using the 'key' variable and the corresponding value using the 'value' variable. This allows you to execute a block of code for each key-value pair in the associative array. Learn more: https://www.php.net/manual/en/control-structures.foreach.php
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *