2015-05-19 26 views
0

我想创建一个JSON编码使用下面的代码,但它包含完整的JSON作为两个独立的项目第二个JSON实体需要包含在属性中。代码创建两个JSON元素时,应该只是一个

//我们只是想在这里使用简单的php json编码,不需要过于复杂。

public function generate_json($table_data) { 
    $result=array(); 
    $result[]=$table_data; 

    if ($table_data) {    
     $result[]= $this->exporter->get_property_gallery_data(20); 
    } 

    return json_encode($result); 
} 


public function get_table_data() { 
    $where = $this->get_query_where(); 
    $query = "SELECT * FROM `#__{$this->export_table}` WHERE 1 " . $where; 
    return wpl_db::select($query, 'loadAssocList'); 
} 

public function get_property_gallery_data($property_id) { 
    $items_table="wpl_items"; 
    $where = $this->get_query_where(); 
    $query = "SELECT * FROM `#__wpl_items` WHERE 1 " .$where.' and parent_id='.$property_id;   

    return wpl_db::select($query, 'loadAssocList'); 
} 

如果粘贴下面的JSON成浏览器可以得到一个例子

+0

什么实际效果,什么是预期的结果? – Med

+0

两个json元素,而不是一个组合的json元素是图库中的物品在一个sperate中应该是正确的{} – rogue39nin

+0

请在您的问题中添加精确的JSON,以及精确的JSON你希望... – Random

回答

0

有两种$结果阵列。 使用array_push将结果推送到单个数组中,然后使用json对结果进行编码

+0

不是它们只有一个 – rogue39nin

0

好的。

下面的代码将这样的伎俩

<?PHP 
    public function generate_json($table_data) { 
     $result=array(); 
     $result1[]=$table_data; 

     if ($table_data) {    
      $result2[]= $this->exporter->get_property_gallery_data(20); 
     } 
     $result[] = array_merge($result1,$result2); 
     return json_encode($result); 
    } 


    public function get_table_data() { 
     $where = $this->get_query_where(); 
     $query = "SELECT * FROM `#__{$this->export_table}` WHERE 1 " . $where; 
     return wpl_db::select($query, 'loadAssocList'); 
    } 

    public function get_property_gallery_data($property_id) { 
     $items_table="wpl_items"; 
     $where = $this->get_query_where(); 
     $query = "SELECT * FROM `#__wpl_items` WHERE 1 " .$where.' and parent_id='.$property_id;   

     return wpl_db::select($query, 'loadAssocList'); 
    } 
?> 
+0

害怕不是它仍然产生两个元素 – rogue39nin

+0

我检查它只给出一个json..Please再次检查 –

相关问题