【问题标题】:Create multidimensional array from serialized array posted with ajax从使用 ajax 发布的序列化数组创建多维数组
【发布时间】:2018-11-14 09:49:12
【问题描述】:

我有一个包含三项内容的列表,首先是整个列表的标题,然后用户可以在每个类别下方动态添加带有问题的类别。

我需要将这些列表添加到我的数据库中,并且当前在发布时有这个数组(使用带有 serializeArray 的 ajax):

Array
(
    [0] => Array
        (
            [name] => lijsttitle
            [value] => Title
        )

    [1] => Array
        (
            [name] => category[]
            [value] => category 1
        )

    [2] => Array
        (
            [name] => question[]
            [value] => question 1
        )

    [3] => Array
        (
            [name] => question[]
            [value] => question 2
        )

    [4] => Array
        (
            [name] => question[]
            [value] => question 3
        )

    [5] => Array
        (
            [name] => category[]
            [value] => category 2
        )

    [6] => Array
        (
            [name] => question[]
            [value] => question 1
        )

    [7] => Array
        (
            [name] => question[]
            [value] => question 2
        )

)

所有问题和类别都已按正确顺序排列,但我希望将它们组合在一起,如下所示:

Array
(
    [title] => Array
        (
            [value] => Title
        )

    [category 1] => Array
        (
            [question] => question 1
            [question] => question 2
            [question] => question 3
        )

    [category 2] => Array
        (
            [question] => question 1
            [question] => question 2
        )
)

或者如果有更好的方法,也许是另一种方法。

我这样序列化我的表单:

$( ".lijstbutton" ).on( "click", function( event ) {
  event.preventDefault();
  url = 'includes/createlist.php';

  $lijst = $( '#lijstform' ).serializeArray();

  var posting = $.post(url, {lijst: $lijst});

    posting.done(function( data ) {
     $( ".lijstresult" ).empty().slideDown('fast').append( data );
   });
  });

为了显示我当前的数组,我在 createlist.php 中有这段代码:

echo '<pre>';
print_r($_POST['lijst']);
echo '</pre>';

最后我需要链接到一个类别的所有问题以及链接到整个列表标题的所有类别。

因此,您可以有多个列表,每个列表都有其类别和这些类别下方的问题。

【问题讨论】:

  • 您是否尝试过编写任何代码来重新组织这个数组?
  • 你如何决定问题的类别?您没有可以提供有关问题类别的详细信息的键值对。
  • @BhaumikMehta 我知道,我该怎么做?我读到您必须在输入字段名称后添加[],但在我的数组中没有添加任何内容,只有空的[]
  • 你能给我们看看 JSON 吗?
  • @Keith 我不认为它是 JSON,但是当我对其进行编码时,您会看到:[{"name":"lijsttitle","value":"sdg"},{"name":"category[]","value":"dsg"},{"name":"question[]","value":"dsgsd"},{"name":"category[]","value":"gsdgsd"},{"name":"question[]","value":"gsd"}]

标签: javascript php arrays ajax


【解决方案1】:

我会重新排列数据。您不能在 array 中多次使用 'question' 作为键。 $arr 是 POST 数据。

$store = [];

// pull off first arr element
$title = array_shift($arr);
// save title to store
$store['title'] = $title['name'];

$currCat = '';
foreach($arr as $a) {
  $val = $a['value'];
  // handle category
  if($a['name'] == 'category[]') {
    // save cat name
    $currCat = $val;
    // init questions array
    $store[$currCat] = [];
  }
  else {
    // add question to question array
    $store[$currCat][] = $val;
  }
}

print_r($store);

【讨论】:

  • 我从上面的代码中得到一个内部服务器错误 500。由于响应与 Ajax 一起使用,我看不到确切的 PHP 错误。
  • 已编辑。有一些错别字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
相关资源
最近更新 更多