create array in foreach loop php code example
Example 1: php foreach
$arr = ['Item 1', 'Item 2', 'Item 3'];
foreach ($arr as $item) {
var_dump($item);
}
Example 2: foreach in php
$arr = array(
'key1' => 'val',
'key2' => 'another',
'another' => 'more stuff'
);
foreach ($arr as $key => $val){
}
foreach ($arr as $key => $val) :
endforeach;
Example 3: foreach in php
<?php
$arr = array("green", "blue", "pink", "white");
foreach ($arr as $element) {
echo "$element ";
}
?>
Example 4: php foreach string in array
foreach($array as $item) {
echo $item['filename'];
echo $item['filepath'];
echo '<pre>'; var_dump($item);
}