2

我已经从Nestable获得序列化,但我不知道如何编写 php 代码更新我的数据库,如wordpress

$data = '[{"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 extract_value($value,$order=0){
        if(is_array($value)){
            foreach($value as $k => $v){
                echo "UPDATE vod_page_menu SET ordering = '$order', parent_id = '$v' WHERE id = '$v'; <br/>";
                extract_value($v,$order+1);
            }       
        }
    }
$json = json_decode($data,true);
extract_value($json);
4

2 回答 2

0
function extract_value($array,$parent=0,$i=1){


        foreach($array as $k => $v) {
            $id = $array[$k]['id'];

            echo "UPDATE vod_page_menu SET ordering = '$i', parent_id = '$parent' WHERE id = '$id'; <br/>";

            if(isset($array[$k]['children'])){

                $children = $array[$k]['children'];
                foreach($children as $key => $obj){
                    $child = $children[$key]['id'];

                    $i++;

                    echo "UPDATE vod_page_menu SET ordering = '$i', parent_id = '$id' WHERE id = '$child'; <br/>";

                    if(isset($children[$key]['children']))
                    extract_value($children[$key]['children'],$child,++$i);

                }

            }   
            $i++;               

        }
    }
于 2013-05-11T02:55:00.557 回答
0

您需要先对 JSON 进行解码,然后才能在 PHP 中使用它:

$data = json_decode($data, true);
foreach ($data as $id => $value) {
...
}
于 2013-05-10T10:01:05.493 回答