JScript code{ identifier: 'name',
  label: 'name',
  items: [
     { name:'陕西省', type:'Province',
         children:[{_reference:'西安'}, {_reference:'延安'}] },
     { name:'西安', type:'city' },
     { name:'延安', type:'city',
         children:[{_reference:'子长县'}, {_reference:'吴起县'}] },
     { name:'子长县', type:'county' },
     { name:'吴起县', type:'county' },
     { name:'北京市', type:'Province', populiation:'921万',
         children:{_reference:'海定区'}},
     { name:'海定区', type:'city',www:' ContentPane.html', populiation:'92万'},
     { name:'江苏省', type:'Province',
         children:[{_reference:'苏州市'}, {_reference:'南京市'}] },
     { name:'苏州市', type:'city',  populiation:'108 万', 面积:'1,972 sq km',
         children:[{_reference:'昆山'}, {_reference:'张家港'}] },
     { name:'昆山', type:'county' },
     { name:'张家港', type:'county' },
     { name:'南京市', type:'city' }
]}
请求帮助,我想要PHP的实现这样的JSON  ,希望高手用PHP在数据库读值实现这样的JSON(dojo tree的实现)。可以建立一个数据库表(也可以建立两个数据库表) 跪求高手帮忙。。

解决方案 »

  1.   

    可以使用PHP的多唯数组实现上面的要求,然后再使用php的函数json_encode进行转换一下。
      

  2.   

    $arrTest = array(
    'identifier'=>'name',
    'label'=>'name',
    'items'=>array(
    'name'=>'陕西省',
    'type'=>'Province',
    'children'=> array( 
    array('_reference'=>'西安'),
    array('_reference'=>'延安'),
    )
    )
    );构造这样的数组,再用json_encode就可以了.
      

  3.   

    这样我都知道。   希望那个高手能写一个在数据库读取数值的php程序,,,因为我不知道怎么写。。并且是实现树形结构的。(谢谢)
      

  4.   

    没明白LZ是要什么  从数据库读出来数据,组合成你想要的数组,然后转成JSON格式,有什么问题
      

  5.   

    在数据读取数据生成1楼那样的json(PHP程序)  然后在页面显示这样的树(dijit.tree)   
      

  6.   

    直接从数据库读取数据出来,拼成json格式,也行得通吧
      

  7.   

    我刚好有个项目做类似的结构。
    希望下面这段代码对你有帮助    /**
         * 
         * get All Records in table "columns"
         * 
         * @return array
         */
        public function getAllRecords()
        {
         $records=array();
         $order="cixu";
         $rowset=$this->fetchAll(null,$order);
         foreach ($rowset as $row){
         $id=$row->id;
         $pid=$row->parentid;
         $name=$row->name;
         $record=array('id'=>$id,'pid'=>$pid,'name'=>$name);
         $records[]=$record;
         }
         return $records;
        }    /**
         * 递归获得columns表中,某个栏目祖先栏目的数目
         
         * @param $array
         * @param $id
         * @return unknown_type
         */
        
        public function getCount($array,$id)
        {
         $count=count($array);
         for($i=0;$i<$count;$i++){
         if($array[$i]['id']==$id){
         $pid=$array[$i]['pid'];
         if($pid!=0){
         $this->_count++;
         $id=$pid;
         $this->getCount($array,$id);
         }else{
         return;
         }
         }
         }
        }
            
        /**
         * get Items prepare to Json data for  Tree
         * @param $checked  如果$checked=0,则生成普通目录树,否则生成带“checked:false”的目录树
         * @return array
         */
        
        public function getColumnsItems($checked=0)
        {
         $records=$this->getAllRecords();
         $count=count($records);
         $children1=array();//存储根目录下的第一层子目录
         //foreach($records as $record){  
            for($i=0;$i<$count;$i++){
                $id=$records[$i]['id'];
                //echo "id=".$id." ";
              // $id=$record['id'];
                if($id){
                    $hasChild=false;
                $children=array();
               // $isZuXian=false;//是祖先吗?
                $this->_count=0;
                    for($j=0;$j<$count;$j++){
                    $pid=$records[$j]['pid'];
                    if($pid){//若pid不为0;  pid=0是祖先目录
                        if($id==$pid){//id从1开始,pid从0开始
                        $ref=array('_reference'=>$records[$j]['name']);
                        $children[]=$ref;
                      $hasChild=true;
                        }//end if
                    }else{//pid为0 
                     $ref=array('_reference'=>$records[$j]['name']);
                     if(!in_array($ref,$children1)){
                         $children1[]=$ref;//根目录下的第一层子目录
                     //$hasChild=true;
                     //    $isZuXian=true;
                     }
                    }
                    }//end for
                  $this->getCount($records,$id);
                    $folder="folder".$this->_count;
                   //echo $folder;
                   
                    if(!$hasChild){
                     if(!$checked){
                      $item=array('name'=>$records[$i]['name'],"type"=>$folder,"id"=>$id);
                     }else{
                     $item=array('checked'=>false, 'name'=>$records[$i]['name'],"type"=>$folder,"id"=>$id);
                     }
                   
                    }else{
                     if(!$checked){
                     $item=array('name'=>$records[$i]['name'],"type"=>$folder,"id"=>$id,"children"=>$children);
                     }else{
                     $item=array('checked'=>false, 'name'=>$records[$i]['name'],"type"=>$folder,"id"=>$id,"children"=>$children);
                     }
                    
                    }//end if
                    $items[]=$item;
                    $pid=$records[$i]['pid'];
                }
            }//end for  
            //print_r($children1);
           // if($isZuXian){
               if(!$checked){
                   $item=array('name'=>"根目录","type"=>"folder","id"=>0,"children"=>$children1);
               }else{
                   $item=array('checked'=>false, 'name'=>"根目录","type"=>"folder","id"=>0,"children"=>$children1);
               }
              
              $items[]=$item;
         //  } 
         return $items;
        }
        
        /**
         * get  JsonData from table "columns"
         * @return Json Data
         */
        
        public function  getColumnsJsonData($checked=0)
        {
         $items=$this->getColumnsItems($checked);
         $identifier='name';
         $label='name';
         $data=new Zend_Dojo_Data($identifier,$items,$label);
         $data=$data->toJson();
         return $data;
        }