foreach key and value code example
Example 1: javascript foreach get key and value
myObject ={a:1,b:2,c:3}
Object.entries(myObject).forEach(([key, value]) => {
console.log(key , value);
});
Object.keys(myObject).forEach(key => {
console.log(key , myObject[key])
})
Example 2: foreach key value javascript
const object1 = {
a: 'somestring',
b: 42
};
for (let [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
Example 3: php foreach
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $val) {
echo "$x = $val<br>";
}
?>
Example 4: js foreach key value
Object.keys(obj).forEach(function (key) {
});
Example 5: for each php
<?php
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list($a, $b)) {
echo "A: $a; B: $b\n";
}
?>