0

Whenever i build a URL i.e.

cart.php?action=add&p[]=29&qty[]=3&p[]=5&qty[]=13

and try to grab the p variable and the qty variable, the = 'Array'

var_dump =

array(3) { ["action"]=>  string(3) "add" ["p"]=>  string(5) "Array" ["qty"]=>  string(5) "Array" } 

I create half the URL with PHP, and the other half is concatenated with Javascript.

1
  • Can you give us the print_r of your $_GET Commented Jan 15, 2010 at 20:42

1 Answer 1

1

P and QTY are Arrays because you created them using the variable[] syntax. And when you try to turn an array into a string, PHP just use's 'Array'. Echoing something turns it into a string, and then prints it to the string.

The [] tells PHP to make a new key in the array numerically, and assign the value to it.

If you want to get acess of the values of p, go like this

foreach($_GET['p'] as $value)
{
     // $value is one of the values of the array, and it goes through all of them
}

The foreach iterates through all of the values of the array, where $value is the value of the current element you are working on.

If you want to access the first value assigned to p, use

echo $_GET['p'][0];
Sign up to request clarification or add additional context in comments.

11 Comments

i understand why they're arrays, it's just every way i've tried to get the values from those arrays has been fruitless. so i my question is more, how to i return the values from those arrays.
I provided methodology for accessing element in the array, could you be more specific?
Oh wait, I guess I didn't read your question clearly enough. Give me a second.
well whats happening right now is if i do the foreach it just spells Array. for instance $_GET['p'][0] = 'A' $_GET['p'][1] = 'r' $_GET['p'][2] = 'r' $_GET['p'][3] = 'a' $_GET['p'][4] = 'y'
You might want to see if using php.net/manual/en/function.parse-str.php will break it apart. You can use $_SERVER['QUERY_STRING'] and parse it.
|

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.