开发者

trying to send array through GET variable, when printed, var equals string of 'Array'

开发者 https://www.devze.com 2022-12-17 01:08 出处:网络
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\'

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.


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消