从动态jquery表单解析json post数据

时间:2013-02-02 06:32:36

标签: jquery json forms parsing dynamic

我正在尝试从动态创建的jquery表单中解析JSON数据。用户可以单击“添加步骤”按钮添加任意数量(或很少)的表单字段(包括附加媒体链接),但我不知道如何在PHP中处理此类数据。以下是我希望收到的一些示例$ _POST数据:

Array
(
    [1] => Array //1, 2, 3, 4 are the position for this particular 'step' on the page
        (
            [Counter] => 1
            [Title] => step one
            [Step] => description
            [Links] => Array
                (
                    [0] => link 1
                    [1] => link 2
                )

        )

    [2] => Array
        (
            [Counter] => 2
            [Title] => step two
            [Step] => some kind of description
            [Links] => Array
                (
                    [0] => link 2
                )

        )

    [3] => Array
        (
            [Counter] => 3
            [Title] => step three
            [Step] => description (another one)
        )

    [4] => Array
        (
            [Counter] => 4
            [Title] => Step four
            [Step] => a lame description
            [Links] => Array
                (
                    [0] => link 1
                    [1] => link 2
                )

        )

    [tutorial_name] => tutorial name? jesus?
    [tutorial_description] => some useless description
    [tutorial_toolsused] => waste of a tools used
    [tutorial_keywords] => waste of keywords
)

知道如何才能最好地处理这些数据(regex,foreach)?我应该避免使用post协议吗?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

您可以使用PHP中的is_array()函数来实现解决方案。

基本上是这样的。

foreach($_POST as $value) {
    if(is_array($value)) {
        //process your data here. i.e. the counter, step, links, title
}

答案 1 :(得分:0)

如果,偶然,任何人都有兴趣。再次感谢!

<?php
foreach($_POST as $value) {
        if(is_array($value)) {
            //process your data here. i.e. the counter, step, links, title
            echo $value['Position'] . "<br /><br />";
            echo $value['Counter'] . "<br /><br />";
            echo $value['Title'] . "<br /><br />";
            echo $value['Step'] . "<br /><br />";
            if(isset($value['Links'])){
                if(is_array($value['Links'])){
                    foreach($value['Links'] as $link){
                        echo $link . "<br /><br />";
                    }
                }
            }
        }
    }
?>