I have a strange result when trying to use a condition inside foreach loop. In a simple array, I would like to point to a specific row (in that case #3). `
$arr = array(
array('id' => 1,'name' => 'Peter'),
array('id' => 2,'name' => 'Ben'),
array('id' => 3,'name' => 'Joe'),
array('id' => 4,'name' => 'Smith'),
array('id' => 5,'name' => 'Josh'),
);
$select = 3;
echo "<br/><br/>";
foreach ($arr as $a) {
echo $a['id'] == $select ? '->' : "" . ' "value="' . $a["id"] . ' "name"=' . $a["name"] . "<br/>";
};
echo "<br/><br/>";
foreach ($arr as $a) {
echo ' "value="' . $a["id"] . ' "name"=' . $a["name"] . "<br/>";
};
The result is as follows:
//First foreach with selection mark
"value="1 "name"=Peter
"value="2 "name"=Ben
-> "value="4 "name"=Smith
"value="5 "name"=Josh
//Second foreach
"value="1 "name"=Peter
"value="2 "name"=Ben
"value="3 "name"=Joe
"value="4 "name"=Smith
"value="5 "name"=Josh`
Any idea is appreciated. Thank you!
source https://stackoverflow.com/questions/75793865/echo-aid-select-blocks-execution-of-the-rest-of-the-line
No comments:
Post a Comment