javascript - JQuery 序列化数组到 PHP 数组 Nesttable jscript

标签 javascript php arrays serialization jquery-nestable

我在网上找到了以下代码。我很乐意在我自己的项目中使用它。

http://dbushell.github.io/Nestable/

这个可拖动的 jquery 生成的树结构生成一个序列化数组。 在我看来,这是一个序列化的 javascript 数组。

[{"id":1,"children":[{"id":3}]},{"id":2,"children":[{"id":4},{"id":9,"children":[{"id":5,"children":[{"id":6},{"id":7},{"id":8}]}]}]},{"id":11},{"id":12,"children":[{"id":10}]}]

对于我能找到的内容,我应该使用 parse_str ,这样就可以了。

但是没有效果。 生成的数组为空。

我尝试了以下测试代码:

   <?php

    $Str = '[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]';

    parse_str($Str, $values);

    print_r($values);

    ?>

我希望任何人都能看到我所忽略的内容。

提前致谢!

Answer!

What I have overlooked is that this is not a Javascript Serialized array but rather a JSON encoded string.

As suggested below I should use JSON decode.

$Str = json_decode('[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]');

This will deliver the result as shown below.

IF I want to use the result as an array instead of what supplied I should use the following function to convert the objects to a valid array:

function toArray($obj){
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }

    return $new;
}

$Str = toArray($Str);

(* this I copied from : How do I convert an object to an array? *)

最佳答案

不,你应该使用json_decode()像这样

<?php

    $Str = '[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]';

    $php_array = json_decode($Str);

    // and just in case there is an error while decoding
    if ( json_last_error() > 0 ) {
        echo json_last_error_msg();
    }

    print_r($php_array);

?>

这将生成输出:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
        )

    [1] => stdClass Object
        (
            [id] => 2
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 3
                        )

                    [1] => stdClass Object
                        (
                            [id] => 4
                        )

                    [2] => stdClass Object
                        (
                            [id] => 5
                            [children] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 6
                                        )

                                    [1] => stdClass Object
                                        (
                                            [id] => 7
                                        )

                                    [2] => stdClass Object
                                        (
                                            [id] => 8
                                        )

                                )

                        )

                    [3] => stdClass Object
                        (
                            [id] => 9
                        )

                    [4] => stdClass Object
                        (
                            [id] => 10
                        )

                )

        )

    [2] => stdClass Object
        (
            [id] => 11
        )

    [3] => stdClass Object
        (
            [id] => 12
        )

)

或者,如果您希望将整个数据集作为数组返回,而不是原始数据中存在的对象,您可以将第二个参数添加到 json_decode($Str, true) ,这一切都将是在数组中:

<?php

    $Str = '[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]';

    $php_array = json_decode($Str, true);

    // and just in case there is an error while decoding
    if ( json_last_error() > 0 ) {
        echo json_last_error_msg();
    }

    print_r($php_array);

?>

给出这个结果:

Array
(
    [0] => Array
        (
            [id] => 1
        )

    [1] => Array
        (
            [id] => 2
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                        )

                    [1] => Array
                        (
                            [id] => 4
                        )

                    [2] => Array
                        (
                            [id] => 5
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                        )

                                    [1] => Array
                                        (
                                            [id] => 7
                                        )

                                    [2] => Array
                                        (
                                            [id] => 8
                                        )

                                )

                        )

                    [3] => Array
                        (
                            [id] => 9
                        )

                    [4] => Array
                        (
                            [id] => 10
                        )

                )

        )

    [2] => Array
        (
            [id] => 11
        )

    [3] => Array
        (
            [id] => 12
        )

)

关于javascript - JQuery 序列化数组到 PHP 数组 Nesttable jscript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31963647/

相关文章:

javascript - 在 reactjs 中,我试图有一个按钮让用户保持登录状态

javascript - gulp-angular-templatecache 没有这样的文件或目录,lstat 'templates.js' 错误

php - 为什么 PHP 没有显示异常?

php - 在狗狗币中转换欧元

javascript - WordPress 中的 BXSlider

javascript - 按键值对 Javascript 中的对象数组进行排序

javascript - 如果用户未登录 Facebook,则 FB.getLoginStatus 不会触发

php - Symfony 表单 handleRequest 未填充

java - 输入不正确

arrays - 向量中集合集合的并集