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

JQuery Serialized array to PHP array Nesttable jscript

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

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
        )

)

只需使用json_decode

print_r(json_decode($Str, true));