0

In the following code I simple feed the values of the form

$t = $_GET['type'];
$p = $_GET['product'];
$b = $_GET['brand'];
$c = $_GET['category'];
$attribute = getattributes($p,$b,$c);
$count = count($attribute);
$i=0;

The variable $attribute has

Array([0]=>size [1]=>weight)

Now using the above array i want to generate the following lines

$size = $_GET['size'];
$weight = $_GET['weight'];

I don't want to assign manually just like the above but create the two lines using loops.

Something like

$i=0;
while($i < $count)
{
    '$'.$attribute[$i] = '$_GET['.$attribute[$i].']'; //edited part
}

I don't know if this will work, could somebody provide a simple piece of code to achieve the same?

Solution Code:

Awlad Liton: Thank for your answer. There is a minor change in your code which gave the desired output (value form the form)

foreach($attribute as $k => $v){
$$v = $_GET[$v];
}
1
  • Yes, the page keep loading after adding the above loop. Commented Jan 30, 2014 at 13:53

2 Answers 2

1

Try this:
Demo: https://eval.in/96388

$attribute = array(0=>'size', 1=>'weight');
foreach($attribute as $k => $v){
    $$v = '$_GET['."'$v'".']';

}
echo $size." ";
echo $weight;

OUTPUT:

$_GET['size'] $_GET['weight']

UPDATE: as you reply you can do this:

foreach($attribute as $k => $v){
  $$v = $_GET[$v];
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Ashik Basheer: i think you wanted this. Did you try this?
You were almost right Awlad. but the output prints $_GET[size] directly. What I wanted is the value. I made a simple change to your code and that works fine :) I'll update the code in my post now.
Accepted :) Is there a way to store them into array? Array([0]=>'value of size' [1]=>'value of weight')
it was array first.After all calculation you want it back again? you can do it array(0=>$size , 1 =>$weight)
1

you are almost right. try this:

$$attribute[$i] = $_GET[$attribute[$i]];

http://php.net/manual/en/language.variables.variable.php

2 Comments

are you kidding me? dreamweaver? - but my fault - remove the brackets - see above
This will not work until you have some query string as ?size=something&weight=something, else it will return undefined index error, so you must check the code in browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.