1

そのため、メニューの構造を含む PHP ページに JQuery 経由で渡された連想配列があります。これは効果的に次のようになります。

[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]

文字列をデコードし、次のように json_decode を使用して連想配列にしました。

$cleanJSON = json_decode($JSON,true);

これまでのところすべて問題なく、次のような結果が得られます。

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

私が抱えている問題は、この新しい構造でデータベースを更新できるように、各項目の左右のネストされたセット値を把握する必要があることです。

これを行っている理由は、ネストされたセット モデル内でメニュー項目の並べ替えを実行できるようにするためです。

以下の例のような結果の配列を取得することは完璧です:

Array (
        [0] => Array
            (
                [id] => 1
                [left] => 1
                [right] => 10
            )
        [1] => Array
            (
                [id] => 2
                [left] => 2
                [right] => 7
            )
        [2] => Array
            (
                [id] => 3
                [left] => 3
                [right] => 4
            )
        [3] => Array
            (
                [id] => 4
                [left] => 5
                [right] => 6
            )
        [4] => Array
            (
                [id] => 5
                [left] => 8
                [right] => 9
            )
    )

以下のコードはめちゃくちゃでまったく機能しませんが、私がそれで得た限りです:

$i_count = 1;
$i_index = 1;
$a_newTree;

function recurseTree($nestedSet) 
{
    global $i_count;
    global $a_newTree;

    $i_left = $i_count;
    $a_newTree[$i_count-1]['left'] = $i_left;
    $i_count++;

    foreach ($nestedSet AS $key => $value)  
    {               
        if ($value['children']) 
          {
              foreach($value['children'] as $a_child)  
              {
                  recurseTree($a_child);      // traverse
              }
          }
    }   

    $i_right=$i_count; // right = count
    $a_newTree[$i_count-1]['right'] = $i_right; 
        $i_count++;        // count+1   
}

どんな助けでも大歓迎です!

4

2 に答える 2

2

解決しました!

友人が作成した気の利いた小さな機能が、この問題を解決してくれました。彼は実際には Javascript で作成しましたが、私はそれを PHP に翻訳しました。以下に両方を提供します。

最初の PHP バージョン:

$JSON = '[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]';
$cleanJSON = json_decode($JSON,true);

$a_newTree = array();       

function recurseTree($structure,$previousLeft) 
{
    global $a_newTree;  // Get global Variable to store results in.

    $indexed = array();                     // Bucket of results.       
    $indexed['id'] = $structure['id'];      // Set ID
    $indexed['left'] = $previousLeft + 1;   // Set Left

    $lastRight = $indexed['left'];

    $i_count = 0;
    if ($structure['children'])
    {
        foreach ($structure['children'] as $a_child)
        {
            $lastRight = recurseTree($structure['children'][$i_count],$lastRight);
            $i_count++;
        }
    }

    $indexed['right'] = $lastRight + 1;     // Set Right

    array_push($a_newTree,$indexed);        // Push onto stack

    return $indexed['right'];       
}

recurseTree($cleanJSON[0],0);
print_r($a_newTree);

素晴らしい小さな関数は、必要な正確な配列を出力します。

OK、私の友人が書いた元の JAVASCRIPT バージョンについては、以下を参照してください。

<html>
   <head>
      <title>Experiment</title>
      <script type="text/javascript">
         /* initial structure */
         var struct = [
            {
                "id": 1,
                "children": [{
                    "id": 2,
                    "children": [{
                        "id": 3
                    }, {
                        "id": 4
                    }]
                }, {
                    "id": 5
                }]
            }
         ];

         function experiment() {
            /* stores all results */
            var all = [];

            /* kick off the recursive method */
            handleNode(struct[0], 0, all);

            /* print the results to browser debugger console*/
            console.log(all);
         }

         function handleNode(node, previousLeft, all) {
            /* create and store the new entry (bucket to put left, right, and id ) */
            var indexed = {};
            all.push(indexed);

            indexed.id = node["id"];
            indexed.left = previousLeft + 1;

            var lastRight = indexed.left;
            /* here we do the recursion for every child */
            for (var x in node["children"]) {
               lastRight = handleNode(node["children"][x], lastRight, all);
            }

            /* once all children have been iterated over we can store the rigth */
            indexed.right = lastRight + 1;

            /* return the newly updated right for this bucket */
            return indexed.right;
         }

         /* run the experiment */
         experiment();

      </script>
   </head>
   <body>

   </body>
</html>

Google Chrome を使用すると、コンソール ウィンドウで結果を確認できます。(CTRL-SHIFT-i)。

于 2013-08-14T08:08:08.470 に答える
0

ネストされたセットの実装を自分でコーディングしようとはしません。使用できるバージョンがあります。

于 2013-08-13T13:04:50.513 に答える