【问题标题】:JQuery Serialized array to PHP array Nesttable jscriptJQuery 序列化数组到 PHP 数组 Nesttable jscript
【发布时间】:2015-08-12 11:17:03
【问题描述】:

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

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);

    ?>

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

提前致谢!

回答!

我忽略的是这不是 Javascript 序列化 数组,而是一个 JSON 编码的字符串。

如下所示,我应该使用 JSON 解码。

$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}]');

这将提供如下所示的结果。

如果我想将结果用作数组而不是我提供的 应使用以下函数将对象转换为有效的 数组:

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);

(* 这是我复制的: How do I convert an object to an array? *)

【问题讨论】:

  • 不要那样使用。它不会遵循 json 解析的标准

标签: javascript php arrays serialization jquery-nestable


【解决方案1】:

不,你应该像这样使用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 就是这样做的,看看输出?或者你说的普通数组是什么意思
  • 结果中类对象的存在是否困扰您?
  • 好吧,如果我尝试将结果用作数组,我会得到:(!)可捕获的致命错误:stdClass 类的对象无法转换为 C:\wamp\www\test\draggable_tree 中的字符串\simple\test2.php 第 8 行
  • 因此,您还需要学习如何寻址对象数组,即$phpArray[1]-&gt;childern[2]-&gt;id 或使用第二个参数将其全部设为数组
【解决方案2】:

只需使用json_decode

print_r(json_decode($Str, true));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2012-09-07
    • 2011-08-11
    • 2014-03-31
    • 1970-01-01
    • 2013-03-01
    • 2011-09-09
    相关资源
    最近更新 更多