如何拆分包含逗号的查询值?

时间:2012-10-20 01:39:40

标签: php url get

  

可能重复:
  How can I split a comma delimited string into an array in PHP?

使用示例网址格式domain.com/?q[]=a_1&q[]=a_2&q[]=a_3

在选择选项中,我将使用以下代码检索selected='selected'

<option value="a_1"<?php if(isset($_GET['q'])) if (in_array('a_1', $_GET['q'])) { echo ' selected="selected"'; } ?>>
Sample 1
</option>
<option value="a_2"<?php if(isset($_GET['q'])) if (in_array('a_2', $_GET['q'])) { echo ' selected="selected"'; } ?>>
Sample 2
</option>
<option value="a_3"<?php if(isset($_GET['q'])) if (in_array('a_3', $_GET['q'])) { echo ' selected="selected"'; } ?>>
Sample 3
</option>

如果网址格式是domain.com/?q=a_1,a_2,a_3,我将如何检索所选值?

2 个答案:

答案 0 :(得分:5)

$vals = explode(',', $_GET['q']);

答案 1 :(得分:1)

你可以爆炸

$arrays = explode(',', $_GET['q']);