Javascript jQuery和JSON:循环JSON数组

Javascript jQuery和JSON:循环JSON数组,javascript,php,jquery,arrays,json,Javascript,Php,Jquery,Arrays,Json,我在数据库中有一个数组: a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";} 所以我用php取消序列化,然后用json编码,代码如下所示: $unwp = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}'); print_r ($unwp); echo json_encode($unwp); 我在

我在数据库中有一个数组:

a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}
所以我用php取消序列化,然后用json编码,代码如下所示:

$unwp = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');

print_r ($unwp);

echo json_encode($unwp);
我在页面上看到:

Array ( [1] => 1993 [2] => 1994 [3] => 1995 [4] => 1996 ) {"1":"1993","2":"1994","3":"1995","4":"1996"}
<?php
   $array = $_POST['inputarray'];
   $str = serialize($array);
   print $str . "\n";
   $unwp = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');
   print_r ($unwp);
   echo json_encode($unwp);
?>

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script>

   jQuery(function ($) {

    // Add children input fields
    var childBlock = $('.block');
    var countChildren = $('div.block div.row').size() + 1;
    $('#addChild').live('click', function (e) {
        e.preventDefault;
        $('<div class="row"><input type="text" name="inputarray['+countChildren+']" id="inputarray'+countChildren+'" placeholder="inputarray['+countChildren+']"><a href="javascript://" id="deleteChild">Delete</a></div>').appendTo(childBlock);
        countChildren++;
    });
    $('#deleteChild').live('click', function (e) {
        e.preventDefault();
        if (countChildren > 2) {
            $(this).parents('div.row').remove();
            countChildren--;
            var counter = 1;
            $('input[name^="inputarray"]').each(function () {
                $(this).attr('name', 'inputarray[' + counter + ']');
                $(this).attr('placeholder', 'inputarray[' + counter + ']');
                $(this).attr('id', 'inputarray' + counter);
                counter++;
            });

        }
    });
})(jQuery);

</script>



   <form action="" method="post">
      <div class="block">
         <div class="row"><input type="text" name="inputarray[1]" placeholder="inputarray[1]"></div>
         <input type="hidden" value="<?php echo $str; ?>">
      </div>
      <input type="submit">
   </form>


<a href="javascript://" id="addChild">Add a child</a>
我需要用jQuery来循环它?所以我可以得到1993199419951996等等。

我正在测试
jQuery.getJSON()
,但不知道如何准确地使用它

所有代码一起显示在页面上:

Array ( [1] => 1993 [2] => 1994 [3] => 1995 [4] => 1996 ) {"1":"1993","2":"1994","3":"1995","4":"1996"}
<?php
   $array = $_POST['inputarray'];
   $str = serialize($array);
   print $str . "\n";
   $unwp = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');
   print_r ($unwp);
   echo json_encode($unwp);
?>

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script>

   jQuery(function ($) {

    // Add children input fields
    var childBlock = $('.block');
    var countChildren = $('div.block div.row').size() + 1;
    $('#addChild').live('click', function (e) {
        e.preventDefault;
        $('<div class="row"><input type="text" name="inputarray['+countChildren+']" id="inputarray'+countChildren+'" placeholder="inputarray['+countChildren+']"><a href="javascript://" id="deleteChild">Delete</a></div>').appendTo(childBlock);
        countChildren++;
    });
    $('#deleteChild').live('click', function (e) {
        e.preventDefault();
        if (countChildren > 2) {
            $(this).parents('div.row').remove();
            countChildren--;
            var counter = 1;
            $('input[name^="inputarray"]').each(function () {
                $(this).attr('name', 'inputarray[' + counter + ']');
                $(this).attr('placeholder', 'inputarray[' + counter + ']');
                $(this).attr('id', 'inputarray' + counter);
                counter++;
            });

        }
    });
})(jQuery);

</script>



   <form action="" method="post">
      <div class="block">
         <div class="row"><input type="text" name="inputarray[1]" placeholder="inputarray[1]"></div>
         <input type="hidden" value="<?php echo $str; ?>">
      </div>
      <input type="submit">
   </form>


<a href="javascript://" id="addChild">Add a child</a>
我会改变

echo json_编码($unwp)


像这样使用
$.getJSON

$.getJSON("scriptname.php", function(data) {
    html = '';
    $.each(data, function(i, el) {
        html += '<div class="block">' +
            '<div class="row"><input type="text" name="inputarray['+i+']" placeholder="inputarray['+i+']"></div>' +
            '<input type="hidden" value="'+el+'">');
    });
    $("form > div").delete();
    $("form").prepend(html);
});
$.getJSON(“scriptname.php”),函数(数据){
html='';
$。每个(数据、功能(i、el){
html+=''+
'' +
'');
});
$(“表单>div”).delete();
$(“表格”)。前缀(html);
});

这可以在PHP中轻松完成。因为我没有看到任何
submit()
click()
的处理程序,也没有任何可能提示ajax请求的处理程序

在同一个文件中也有php,那么为什么不简单地使用php循环并生成所需的内容呢

echo "<select name='year'>";
foreach($unwp as $year) {
    echo "<option value='{$year}'>{$year}</option>";
}
echo "</select>";
echo”“;
foreach(unwp为$year){
回声“{$year}”;
}
回声“;
上面的代码片段将完全满足您的需要


编辑


您正在尝试生成一个
对吗?如果没有,请告诉我,以便我可以根据需要进行修改。

当您已经在php脚本中获得结果时,为什么还要使用jquery循环?请您更清楚一点,以便我们能够理解它背后的动机。如果这是您现在正在编写的内容,您不应该使用迁移插件,而仍然使用
live
,您应该转到
on
。此外,您应该使用
length
而不是
size()
。第三,你只是在页面上回显JSON,你能把它回显到一个javascript变量吗,或者你真的需要用ajax来获取它吗?我有一个项目,我可以创建无限的动态文本字段。然后从中获取所有数据,将其存储到数据库中。然后从数据库中的这个数组中获取所有结果,这样我就可以得到类似这样的结果:Has 4 children(1993199419951996)。将其更改为var fromPHP=$.getJSON(“.”.json_encode…在您的示例中,如果该示例不会编译(因为字符串周围没有),那么fromPHP变量将只包含一个json字符串。它会起作用,它不会将字符串传递给fromPHP,而是传递对象,因此getJSON不必要是我的错误。采用这种方法没有什么意义(如果脚本在同一个文件中,我该如何使用它呢?我以为您想动态添加输入。如果脚本在同一个文件中,您可以只编写一个PHP循环,而不需要在jQuery中执行。现在我自己也很困惑:)那么我该如何使用PHP进行循环?
foreach($i=>unhp$value){echo…}
我已经这样修改过了:
foreach($unwp as$year){$countYears++;echo$year.',';}
。如何删除数组中最后一个值后的逗号?@AlexChizhov,他不仅仅做
echo introde(',',$unwp);