Log in

View Full Version : Dynamically change a variables name



jc_gmk
10-25-2007, 02:41 PM
Is it possible to change the name of the variable dynamically

e.g.

I want to set these variables on a page:


$id1 = $_GET['id1'];
$id2 = $_GET['id2'];
$id3 = $_GET['id3'];
$id4 = $_GET['id4'];
etc...


only I don't know how many of these I will have on the page as the number will be generated from the previous page. could be 4 or 100.

I think i need some kind of loop such as

for ($i=1; $i<=$qty; $i++)
{
$id* = $_GET['id*'];
}
I want to change $id* to $id1, $id2 ,$id3, $id4 etc...
depending on the value of $qty

does anyone know if this is even possible? Or if i'm making any sense!

boogyman
10-25-2007, 02:45 PM
for ($i=1; $i<=$qty; $i++)
{
$id.$i = $_GET['id'.$i];
}

jc_gmk
10-25-2007, 03:03 PM
Sweet! easy as that.

Thanks! :)

djr33
10-25-2007, 06:20 PM
Ah, I wasn't aware that was valid syntax. Thanks for that, boogyman.

The method I have always used is "variable variables", like this:

$$name = 1;

So....
$name = 'test';
$$name = 1;
echo $test; //1

If you need to have a more complex variable name, you'll just need to create it in the previous line:

$name = $a.'b'.$c.'abc';
$$name; //use as you want


Though $a.$b may work, be cautious when modifying that syntax as some forms do not work. $$var will always work, and refer to the variable named the value of $var.

boogyman
10-25-2007, 06:29 PM
Ah, I wasn't aware that was valid syntax. Thanks for that, boogyman.

The method I have always used is "variable variables", like this:

$$name = 1;

So....
$name = 'test';
$$name = 1;
echo $test; //1

If you need to have a more complex variable name, you'll just need to create it in the previous line:

$name = $a.'b'.$c.'abc';
$$name; //use as you want


Though $a.$b may work, be cautious when modifying that syntax as some forms do not work. $$var will always work, and refer to the variable named the value of $var.
I have never had any problems with doing it that way. Whether its technically valid or not you would probably have to refer to either guru that is Twey / John.
I can understand your method of assigning the variable by reference, but personally concantenating the variables are easily for me.. well at least in this instance. Its really no different than if you were doing



for($i=0; $i<max; $i++)
{
$var[$i] = something;
}

Twey
10-25-2007, 06:50 PM
$id.$i = $_GET['id'.$i];This will not work, at least in PHP5. Instead, it will be interpreted as $id . ($i = $_GET['id' . $i]). djr33's method is the correct one, but any use of variable variables is almost certainly an indicator of bad code. The simplest way of accomplishing what you want is:
foreach($_GET as $k => $v)
if(strpos($k, 'id') === 0)
$$k = $v;However, I would strongly suggest:Not doing this; this style of codingopens up several security risks for which you must plan if you intend to use it; clutters up the global namespace; will not allow autoglobal access to these variables inside functions, so you'd have to do
function foo() {
global $id1;
global $id2;
global $id3;
global $id4;
}... or equivalent inside your functions.If you find $_GET['id4'] too much to type, you can always copy $_GET to another variable:
$g = $_GET; Using an array for these values; according to the CGI specification simply having multiple elements in your form with the same name should be enough to cause the value to be an array as handled in your server-side script, but PHP is somewhat broken in respect to this specification, and therefore when passing values to it in an array you must end the names with []:
<input type="text" name="ids[]">
<input type="text" name="ids[]">
<input type="text" name="ids[]">
<!-- ... -->$_GET['ids'] will then be an array containing all these values, which you can manipulate using normal array functions.
Whether its technically valid or not you would probably have to refer to either guru that is Twey / John.As far as I'm aware, John knows no PHP. I envy him this somewhat :)
Its really no different than if you were doing [an array assignment]It is very different, primarily in that the syntax is ambiguous (and apparently differs between versions) and also in that it relies upon a deprecated feature, namely the interpretation of id as a string rather than an undefined constant, to work.

jc_gmk
10-26-2007, 08:19 AM
Using an array for these values; according to the CGI specification simply having multiple elements in your form with the same name should be enough to cause the value to be an array as handled in your server-side script, but PHP is somewhat broken in respect to this specification, and therefore when passing values to it in an array you must end the names with []:
<input type="text" name="ids[]">
<input type="text" name="ids[]">
<input type="text" name="ids[]">
<!-- ... -->$_GET['ids'] will then be an array containing all these values

This was exactly what i was looking to do, thanks!
Looked as though I was trying to do it the hard way!

Following on from that though, I have now collected two differant arrays using this method

e.g.
$id = array( [0] => 100 [1] => 221 [2] => 343 );
and
$qty = array( [0] => 3 [1] => 6 [2] => 1 );

Am i able to merge them?
I need to end up with an array like this:

$cart = array(100,100,100,221,221,221,221,221,221,343);

Notice that the first value "100" matches up with the "3" and prints "100,100,100" and "221" to the "6" and so on...

Twey
10-26-2007, 09:27 AM
$cart = array();
foreach($id as $k => $v)
for($i = 0; $i < $qty[$k]; ++$i)
$cart[] = $v;

djr33
10-26-2007, 09:37 AM
There are also simple functions, like array_merge(), but I'm not sure that's customizable enough for your current purposes.

jc_gmk
10-26-2007, 09:38 AM
No luck, if the first value of $id is "100" and $qty is "6" it just prints out this array:

Array ( [0] => 600 [1] => 0 [2] => 0 [3...

i would need it to look like this:

Array ('100','100','100','100','100','100')

Twey
10-26-2007, 09:40 AM
I edited it, I misunderstood you at first. Try this one.
There are also simple functions, like array_merge(), but I'm not sure that's customizable enough for your current purposes.Not as far as I'm aware.

jc_gmk
10-26-2007, 09:46 AM
Tried it, still no luck.

Although I noticed that if the first value of $id is "100" and $qty is "6"

it will print
Array ( [0] => 600 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 )

Whereas if the first value of $id is "100" and $qty is "2"

it will print
Array ( [0] => 200 [1] => 0 )

Twey
10-26-2007, 09:47 AM
Still looks like you're using the old code to me.

jc_gmk
10-26-2007, 09:55 AM
oops, yeah I had both in there!

The new one is much better although it prints

Array ( [0] => 100 [1] => 100 [2] => 100 [3] => 100 [4] => 100 [5] => 100 )

Which is almost what i want but without the "[0] =>"

but instead the value just seperated with commas

Array ('100','100','100','100','100','100')

djr33
10-26-2007, 10:07 AM
That's harder because PHP doesn't output that automatically.

function array_out($a) {
echo 'Array (\'';
echo implode('\',\'',$a);
echo '\')';
}

jc_gmk
10-26-2007, 10:13 AM
Do you know how i'd put that in relation to this: ?


$cart = array();
foreach($id as $k => $v)
for($i = 0; $i < $qty[$k]; ++$i)
$cart[] = $v;

I'm assuming that i'd replace $a with $cart?
e.g.


function array_out($cart) {
echo 'Array (\'';
echo implode('\',\'',$cart);
echo '\')';
}

Twey
10-26-2007, 10:19 AM
Why are you printing an array like that? The string representation of an array is only meant for debugging purposes.

djr33
10-26-2007, 10:20 AM
function name($argument) { ....stuff.... }

That creates a function.

For $cart, you'd use array_out($cart); and that would execute the commands in it.

Alternatively, you could just use the three lines from inside the function and replace $a in the second with $cart, as you did in the last code block (but then you don'tneed the first or last line)

jc_gmk
10-26-2007, 10:38 AM
Perfect, thanks once again fellas!

My final code ended up like this:


$temp = array();
foreach($id as $k => $v)
for($i = 0; $i < $qty[$k]; ++$i)
$temp[] = $v;

$cart = "array ('". implode("','",$temp)."')";

djr33
10-26-2007, 11:13 AM
That confused me for a minute because it lacks brackets. I'd add them in for clarity.

foreach {
for {
...
}}

Twey
10-26-2007, 11:19 AM
It's a lot less confusing when indented properly. As for your braces, that's even more confusing, because it's still flat and you've put your braces in a configuration that doesn't make sense to anybody :-\

jc_gmk
10-26-2007, 11:37 AM
Yes I have already changed it to:


$temp = array();
foreach($id as $k => $v)
{
for($i = 0; $i < $qty[$k]; ++$i)
{
$temp[] = $v;
}
}
$cart = "array ('". implode("','",$temp)."')";


However, I have come across another problem going back a few steps.

I initially created this $id array from a form that sends all the values from a hidden input field e.g.

<input type="hidden" name="id[]" value="$variable" />
Because the form uses $_GET, it adds each value to the url.

However if I have say 200 values to get, it cuts the array off early. probably because the url if far too long.

Is there any way around this?

Twey
10-26-2007, 11:49 AM
Use POST.

jc_gmk
10-26-2007, 02:04 PM
already tried that but it didn't work! thought it was something to do with the way the form values set the array.

Got it to work now though.

Thanks for all the effort! :)