0

次のコードをオンラインで見つけました。そして、自分のプロジェクトでそれを使用したいと思っています。

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

(* これからコピーしました: オブジェクトを配列に変換するにはどうすればよいですか? *)

4

2 に答える 2

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
        )

)

または、元のデータに存在するオブジェクトではなく、データセット全体を配列として返したい場合は、2 番目のパラメーターを追加する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
        )

)
于 2015-08-12T11:18:51.407 に答える