开发者

Can't retrieve variables from url

I can't seem to get the 'required_ids' array in my url to be passed into a php variable. Basically I just want to count the size of the array in php so I can put that number in a DB. The problem I have is getting the url string into a variable to pull the requested_ids out.

http://www.somewebsite.com/somedirectory?sk=14058889342675&requested_ids%5B0%5D=51630110&r开发者_如何学Cequested_ids%5B1%5D=19109453492

Any ideas how I can quickly count the number of requested_ids? Thanks


If you use this to see what is in $_GET (which contains the values passed in the URL) :

var_dump($_GET);

You'll get the following kind of output :

array
  'sk' => string '14058889342675' (length=14)
  'requested_ids' => 
    array
      0 => string '51630110' (length=8)
      1 => string '19109453492' (length=11)


Which means you can use $_GET['requested_ids'] to know what is in that array.

And, using count() on that array :

var_dump(count($_GET['requested_ids']));

You will get :

int 2


Basically, when values are passed in the query-string using [], like this :

requested_ids[0]=51630110&requested_ids[1]=19109453492

They end up in a PHP array, in $_GET, called requested_ids.


You can try using PHP's count() on $_GET['recrivied_ids'] array. Or you can serialize(very useful) requested_ids array before sending and send it via _GET. It should be easier than array in URL.

Example:

  echo 'Parameters number: ' . count($_GET['requested_ids']);

When serialized:

  $requested_ids = unserialize($_GET['requested_ids');
  echo count($requested_ids);

Hope it helps. ;)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜