根据其名称将序列化的JS数组分组为相似的数组

时间:2019-05-15 12:46:39

标签: javascript php jquery forms

我用$('#myForm').serializeArray();序列化了表格。我得到了所有输入,但是我的表单很复杂,需要将相同的名称分组。

在我的ajaxController中:

$data = json_decode($request->getParameter('data'));    
print_r($data);

这是输出结果:

array (size=8)
  0 => 
    object(stdClass)[192]
      public 'name' => string 'evaluation_candidature[id_evaluateur_candidature]' (length=49)
      public 'value' => string '2899' (length=4)
  1 => 
    object(stdClass)[196]
      public 'name' => string 'evaluation_candidature[_csrf_token]' (length=35)
      public 'value' => string '51c7f3442410a04e926c7a7abd1082d9' (length=32)
  2 => 
    object(stdClass)[197]
      public 'name' => string 'evaluation_candidature[note_globale]' (length=36)
      public 'value' => string 'A+' (length=2)
  3 => 
    object(stdClass)[198]
      public 'name' => string 'evaluation_candidature[appreciation]' (length=36)
      public 'value' => string 'je teste l'évaluation
TB!' (length=27)
  4 => 
    object(stdClass)[199]
      public 'name' => string 'evaluation_candidature[Apport pour le laboratoire_1][note]' (length=58)
      public 'value' => string '1' (length=1)
  5 => 
    object(stdClass)[200]
      public 'name' => string 'evaluation_candidature[Apport pour le laboratoire_1][id_evaluateur_candidature]' (length=79)
      public 'value' => string '2899' (length=4)
  6 => 
    object(stdClass)[201]
      public 'name' => string 'evaluation_candidature[Apport pour le laboratoire_1][evaluation_critere_id]' (length=75)
      public 'value' => string '91' (length=2)
  7 => 
    object(stdClass)[202]
      public 'name' => string 'evaluation_candidature[Apport pour le laboratoire_1][remarque]' (length=62)
      public 'value' => string '' (length=0)

我需要将所有evaluation_candidature[Apport pour le laboratoire_1]分组,也需要将evaluation_candidature分组。

问题是我事先不知道名字。名称可能会有所不同,因为它取决于表单集合。我希望有人能帮助我。

我尝试将javascript中的更新分组:

var datas = [],
        serialized = $('#myForm').serializeArray(),
        i;
    for (i=0; i<serialized.length; i+=2) {
        var tmpObj = {};
        tmpObj[ serialized[i].name ] = serialized[i].value;
        tmpObj[ serialized[i+1].name ] = serialized[i+1].value;
        datas.push(tmpObj);
    }

    console.log(datas);

这里的输出:

array (size=6)
  0 => 
    object(stdClass)[192]
      public 'evaluation_candidature[id_evaluateur_candidature]' => string '2899' (length=4)
      public 'evaluation_candidature[_csrf_token]' => string '51c7f3442410a04e926c7a7abd1082d9' (length=32)
  1 => 
    object(stdClass)[196]
      public 'evaluation_candidature[note_globale]' => string 'A+' (length=2)
      public 'evaluation_candidature[appreciation]' => string 'je teste l'évaluation
TB!' (length=27)
  2 => 
    object(stdClass)[197]
      public 'evaluation_candidature[Apport pour le laboratoire_1][note]' => string '1' (length=1)
      public 'evaluation_candidature[Apport pour le laboratoire_1][id_evaluateur_candidature]' => string '2899' (length=4)
  3 => 
    object(stdClass)[198]
      public 'evaluation_candidature[Apport pour le laboratoire_1][evaluation_critere_id]' => string '91' (length=2)
      public 'evaluation_candidature[Apport pour le laboratoire_1][remarque]' => string '' (length=0)

如您所见,我仍然有重复。

1 个答案:

答案 0 :(得分:0)

我100%确定您需要PHP的array_filter($ your_array,“ callBackFunction”)。在您通过JS收集表单详细信息之后,将其传递给PHP,并具有一个回调函数,以便使用您的函数或内置函数(例如array_sort(),array_unique(),.......)按所需方式对其进行排序。

    Arrays.sort(fList, Comparator.comparing(File::getName));
    Arrays.sort(fList, NameFileComparator.NAME_COMPARATOR);

或者如果您想通过KEY来完成

$sorted_array = array_filter(
    $my_array, 
    function($value) {
        // your code to filter through and group them
        foreach() {
            $new_group_array[$key] = $value;
        }
        return $new_grouped_array;
    }
);