I get ajax like this:

<option value='0'>Центральный район</option> <option value='1'>Невский район</option> <option value='2'>Адмиралтейский район</option> <option value='3'>Василеостровский район</option> 

All this is in the data variable, and I need to parse all the names (for example: Pushkin District, etc.) into the array var option = ['Pushkin District', ['etc.)]]

Help please build a regular season.

  • Why don't you use JSON in this case? - oleg42
  • Do not quite understand how. There everything is generated like this: $ ray = explode ("\ n", $ rayon ['data'] ['initial']); for ($ i = 0; $ i <18; $ i ++) {echo ("<option value = '{$ i}'> {$ ray [$ i]} </ option> \ r \ n"); } I don’t know how to turn this into JSON, and even if I know, there will be more hassle with javascript, but it's easier for me to do everything in PHP. - chuikoff
  • The horror is terrible ... Morka on js is just building a regexp and processing the array for the second time on the client. Now I will answer the answer - oleg42
  • AJAX request is done via jQuery? - oleg42
  • YES! Through jQuery! - chuikoff

3 answers 3

 "success": function(resp) { window.list = []; $('<select>').html(resp).find('option').each(function(){ // Если массив обрабатывается 1 раз, то это можно сделать тут же, без создания переменной window.list.push($(this).text());}) }); } 
  • Thank you very much. I will understand further. - chuikoff
 var res, result=[],str, reg; str ="<option value='0'>Центральный район</option><option value='1'>Невский район</option><option value='2'>Адмиралтейский район</option><option value='3'>Василеостровский район</option>"; reg = />([^<>]+)</g; while ( (res = reg.exec(str)) != null) { result.push(res[1]); } 

// result contains the desired array

    It is stupid to try * JS code, if you use JSON + to change the PHP code, it is more convenient

     $ray = explode("\n",$rayon['data']['initial']); $result = array(); for($i=0;$i<18;$i++){ $result[$i] = $ray[$i]; } return $result; А вот что должно быть в JS -> (не видел ваш код, поэтому покажу пример с POST запросом ) $.post("обработчик.php", {данные которые передаете}, function(data){ for(var i=0;i<18;i++) { alert(data[i]); } }, "json");