我从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);
答案 0 :(得分:0)
您需要先解码JSON,然后才能在PHP中使用它:
$data = json_decode($data, true);
foreach ($data as $id => $value) {
...
}
答案 1 :(得分: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++;
}
}