note 126036 added to language.variables.variable
| From: | derek at deperu dot com | Date: | Fri, 23 Apr 2021 19:14:49 +0000 |
| Subject: | note 126036 added to language.variables.variable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216319@lists.php.net to get a copy of this message | ||
How to use variable variables with for and foreach():
<?php
$obj_1 = array("one" => "One", "two" => "Two",
"three" => "Three");
$obj_2 = array("one" => "Four", "two" => "Five",
"three" => "Six");
$obj_3 = array("one" => "Seven", "two" => "Eight",
"three" => "Nine");
for ($i=1; $i < 4; $i++) {
foreach (${"obj_".$i} as $key => $value) {
echo $key . ': ' . $value."\n";
}
}
?>
Result:
one: One
two: Two
three: Three
one: Four
two: Five
three: Six
one: Seven
two: Eight
three: Nine
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 190.43.94.167)
----
Manual Page -- https://php.net/manual/en/language.variables.variable.php
Edit -- https://main.php.net/note/edit/126036
Del: integrated -- https://main.php.net/note/delete/126036/integrated
Del: useless -- https://main.php.net/note/delete/126036/useless
Del: bad code -- https://main.php.net/note/delete/126036/bad+code
Del: spam -- https://main.php.net/note/delete/126036/spam
Del: non-english -- https://main.php.net/note/delete/126036/non-english
Del: in docs -- https://main.php.net/note/delete/126036/in+docs
Del: other reasons-- https://main.php.net/note/delete/126036
Reject -- https://main.php.net/note/reject/126036
Search -- https://main.php.net/manage/user-notes.php