tags:

views:

191

answers:

1

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, they = '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. If more code is needed please let me know!

+1  A: 

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];
Chacha102
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.
mlebrun15
I provided methodology for accessing element in the array, could you be more specific?
Chacha102
Oh wait, I guess I didn't read your question clearly enough. Give me a second.
Chacha102
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'
mlebrun15
Ok, so for some reason it is the word array instead of being an actual array. Do you do anything weird to `$_GET`?
Chacha102
mlebrun15
I have no idea. That just seems weird.
Chacha102
You might want to have it submit `POST` data instead of creating a GET url. POST Data is more apt to arrays of data than GET.
Chacha102
exactly. i mean as long as it remains a string the whole time and everything is correct in the string it should work? but no matter what i try, it prints 'Array' as the variable
mlebrun15
the only problem with that is that my boss built this whole shopping cart based on GET. don't have the time frame to switch everything up
mlebrun15
You might want to see if using http://www.php.net/manual/en/function.parse-str.php will break it apart. You can use `$_SERVER['QUERY_STRING']` and parse it.
Chacha102
that worked perfectly! thank you so much, chacha!
mlebrun15