Hello, it comes to me with $ .post, this is the line -

'options_cities' => '[\"1\",\"2\"][\"15\",\"16\"]' 

Tell me, can php be used to make an array out of it? Do not pay attention to groups of elements, only numbers are needed. This type:

 'options_cities' => [ 0 => '1' 1 => '2' 2 => '15' 3 => '16' ] 
  • 3
    If you explain what is the general connection between the data at the input and what should be obtained at the output - will be a subject for discussion. In the meantime, it is not traceable. - PinkTux
  • By the line, if I'm not mistaken, you can by default be treated as an array. - ultimatum
  • The set of id elements comes as a string, I need to write each id as a separate one in the database - Vlad Shkuta
  • 2
    You showed a certain list of values ​​in square brackets at the input, I see 4 groups of brackets and you say that at the output 4 elements are ok. But how do you say from [1,7] that you propose to get the number 86, I cannot imagine - Mike
  • A little wrong put the question, updated. You do not need to pay attention to the brackets, only numbers - Vlad Shkuta

3 answers 3

Use preg_split :

 $arr = preg_split("/[^0-9]+/", '[\"1\",\"2\"][\"15\",\"16\"]', NULL, PREG_SPLIT_NO_EMPTY); 

    There are 3 differences from normal JSON:

    1. Not needed shielding.
    2. Arrays are not separated by a comma.
    3. Many arrays are right in the root.

    Like that:

     $str = '[\"1\",\"2\"][\"15\",\"16\"]'; // Убираем экранирование, делим по ] и собираем уже с запятой $str = implode('],', explode(']', str_replace("\\", '', $str))); // ["1","2"],["15","16"], // Помещаем в литералы массива и обрезаем лишнюю запятую в конце $str = '[' . substr($str, 0, -1) . ']'; // [["1","2"],["15","16"]] // Преобразуем в массив $str = json_decode($str); 

    But this is only for sample input.
    If there is another format, this may not help.
    To do this, write parsers, and this is just string processing.

      You look aside f-and explode , using dividers, it is possible to convert a line to an array easily.