How can you efficiently loop through an array with non-sequential keys in PHP?

When looping through an array with non-sequential keys in PHP, it is more efficient to use a foreach loop rather than a traditional for loop. This is because a foreach loop can automatically handle non-sequential keys without the need for manual indexing or calculations. By using a foreach loop, you can iterate through the array elements in a more straightforward and efficient manner.

$array = [
    'key1' => 'value1',
    'key3' => 'value3',
    'key5' => 'value5'
];

foreach ($array as $key => $value) {
    echo "Key: $key, Value: $value\n";
}