Опубликовать данные с помощью Json преобразовать в массив - PullRequest
0 голосов
/ 05 июня 2018

Здравствуйте, у меня есть этот сценарий с рекурсией, я пытаюсь оценить переменные в $_POST с идеей извлечения тех, которые имеют формат Json:

Отправленные данные:

array(1) {
  ["jsonData"]=>
  string(159) "[{"s_file_1":"Prueba 3"},{"text_file_1":"ORT0000133.pdf"},{"s_file_2":"Prueba 5"},{"text_file_2":"factura.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]"
}

Скрипт для оценки данных:

public function BuildV($_VARS) {
        if (is_array($_VARS)) {
            foreach ($_VARS as $val) {
                $chk = $this->BuildV($val);
                if ($chk) {
                    return json_decode($val, true);
                } else {
                    return $val;
                }
            }
        } else {
            return $chk = (json_decode($_VARS) != NULL) ? true : false;
        }
   }

$_VARS = $_POST;
$_VARS = $this->BuildV($_VARS);

Результат:

array(6) {
  [0]=>
  array(1) {
    ["s_file_1"]=>
    string(8) "Prueba 3"
  }
  [1]=>
  array(1) {
    ["text_file_1"]=>
    string(14) "ORT0000133.pdf"
  }
  [2]=>
  array(1) {
    ["s_file_2"]=>
    string(8) "Prueba 5"
  }
  [3]=>
  array(1) {
    ["text_file_2"]=>
    string(11) "factura.pdf"
  }
  [4]=>
  array(1) {
    ["idform"]=>
    string(10) "f-gen-desk"
  }
  [5]=>
  array(1) {
    ["idprocess"]=>
    string(6) "p-save"
  }
}

но мне нужен этот формат:

array(4) {
  ["s_file_1"]=>
  string(8) "Prueba 3"
  ["text_file_1"]=>
  string(14) "ORT0000133.pdf"
  ["s_file_2"]=>
  string(8) "Prueba 5"
  ["text_file_2"]=>
  string(11) "factura.pdf"
}

как я могу упроститьили исключить индексы массива, которые начинаются с нумерации.

All the Input will have a structure:
name='i_text_####'; # for input type text
name='i_num_####'; # for input type num
name='s_text_####'; # for input type select option
name='t_text_####'; # for input type textarea
name='ch_text_####'; # for input type checkbox

Символ # в тегах имени указывает количество повторений.

1 Ответ

0 голосов
/ 05 июня 2018

Я добавил это к сценарию, чтобы решить:

$_VARS = $_POST;
$_VARS = $this->BuildV($_VARS);
$_VARS = array_reduce($_VARS, 'array_merge', array());

Получить этот вывод:

array(6) {
  ["s_file_1"]=>
  string(8) "Prueba 3"
  ["text_file_1"]=>
  string(14) "ORT0000133.pdf"
  ["s_file_2"]=>
  string(8) "Prueba 5"
  ["text_file_2"]=>
  string(11) "factura.pdf"
  ["idform"]=>
  string(10) "f-gen-desk"
  ["idprocess"]=>
  string(6) "p-save"
}
...