Get grouped HTML data into a PHP array? (Similar to sub-elements of a check box)

How to convert this form structure to a PHP array? I can also edit HTML, so every idea is good ...

enter image description here

If the checkbox is selected, then this data should be inserted into the PHP array by saving, the necessary structure:

0 =>
   'group' => 'group_a',
   'type' => 'type_yellow',
   'desc' => 'now to convert'
1 =>
   'group' => 'group_b',
   'type' => 'type_orange',
   'desc' => 'needed to order'

As an example, I tried to specify inputthe element name as arrayhow description[], but I can not control whether the checkbox is checked or not ...

+4
source share
1 answer

Hm. For flags, standard behavior is a value that is sent only if the flag is checked. Thus, a solution for the server side for this could be:

-, :

  • entries[][group]
  • entries[][type]
  • entries[][desc]

,

  • entries[0][group]
  • entries[0][type]
  • entries[0][desc]
  • entries[1][group]
  • entries[1][type]
  • entries[1][desc]
  • .....

$_POST[entries], POST.

$entries = $_POST['entries'];

entries, . array_filter :

$entries = array_filter($entries, function($entry) {
    return isset($entry['group']);
});
+2

Source: https://habr.com/ru/post/1670718/


All Articles