-2

Possible Duplicate:
How can I split a comma delimited string into an array in PHP?

With the example URL format domain.com/?q[]=a_1&q[]=a_2&q[]=a_3:

In select option I would retrieve the selected='selected' using this code:

<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>

If the URL format were instead domain.com/?q=a_1,a_2,a_3, how would I retrieve the selected value?

Community
  • 1
  • 1
Unknown Error
  • 767
  • 6
  • 14
  • 25
  • Either make it an array (str_getcsv) first, or search for string parts (strstr) instead. – mario Oct 20 '12 at 01:43

2 Answers2

5
$vals = explode(',', $_GET['q']);
Musa
  • 96,336
  • 17
  • 118
  • 137
Marc B
  • 356,200
  • 43
  • 426
  • 500
1

You can explode it

$arrays = explode(',', $_GET['q']);
codingbiz
  • 26,179
  • 8
  • 59
  • 96