This is a handy function I put together to allow variable
variables to be used with arrays.
To use the function, when you want to reference an array,
send it in the form 'array:key' rather than 'array[key]'.
For example:
<?php
function indirect ($var, $value) // Replaces $$var =
$value
{
$var_data = $explode($var, ':');
if (isset($var_data[1]))
{
${$var_data[0]}[$var_data[1]] = $value;
}
else
{
${$var_data[0]} = $value;
}
}
$temp_array = array_fill(0, 4, 1);
$temp_var = 1;
$int_var_list = array('temp_array[2]', 'temp_var');
while (list($key, $var_name) = each($int_var_list))
{
// Doesn't work - creates scalar variable called
"$temp_array[2]"
$$var_name = 0;
}
var_dump($temp_array);
echo '<br>';
var_dump($temp_var);
echo '<br>';
// Does work!
$int_var_list = array('temp_array:2', 'temp_var');
while (list($key, $var_name) = each($int_var_list))
{
indirect($var_name, 2);
}
var_dump($temp_array);
echo '<br>';
var_dump($temp_var);
echo '<br>';
?>
----
Server IP: 87.124.64.96
Probable Submitter: 87.194.245.158
----
Manual Page -- http://www.php.net/manual/en/language.variables.vari
able.php
Edit -- https://master
.php.net/note/edit/78466
Del: integrated -- h
ttps://master.php.net/note/delete/78466/integrated
Del: useless -- http
s://master.php.net/note/delete/78466/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78466/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78466/spam
Del: non-english --
https://master.php.net/note/delete/78466/non-english
Del: in docs -- http
s://master.php.net/note/delete/78466/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78466
Reject -- https://mast
er.php.net/note/reject/78466
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|