|
Posted by J.O. Aho on 08/05/06 21:16
Dave wrote:
> Hi,
> I'm reasonably proficient in PHP but have been asked how to do
> something which has got me stumped. Hence, my posting:
>
> I want to create a small number of variables, $pos1, $pos2 ... $pos5
> within a loop and assign them values from an array, but creating the
> variable names with incrementing numbers, well I've no idea how the PHP
> syntax for it works.
>
> ========== START CODE ==========
> <?php
>
> $x=5;
> $myarray = array('a','b','c','d','e');
>
> for($a=0;$a<$x;$a++){
/*Create a value stored in a variable, may be unneeded but for safety*/
$variable_name='pos'.$a;
/* we tell to use the name inside the variable as the new var name */
$$variable_name = $myarray[$a]; //Create $pos0 here on first loop,
> // $pos1 next loop and so forth..
> }
>
> //Test assigned variable
> echo $pos3; // $pos3 should equal 'd'
> exit;
>
> ?>
>
> ========== END CODE ==========
>
> Any ideas?
You can check http://www.php.net/manual/en/language.variables.variable.php for
more info about how to create variable variable names.
//Aho
Navigation:
[Reply to this message]
|