'How to properly loop through array keys in PHP [closed]
If I have an array with multiple keys and values, like this:
$array = array(
'key1' => 'value1',
'key2' => 'value2',
);
Are there any best-practices on how to loop through an array when I only use the keys?
Possible solution 1:
foreach(array_keys($array) as $array_key) {
echo $array_key;
}
Possible solution 2:
foreach($array as $array_key => $array_value) {
echo $array_key;
}
I can see the advantage in solution 1 as it doesn't include the unused variable $array_value. Is there a difference in performace (speed, memory allocation, etc.) between these two?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
