2013-07-29 91 views
2

我在codeigniter中执行我的项目。我的问题是我会为'game_aspect_details' JSON格式一样json在codeigniter中对数组进行编码和解码

"{"game_aspect_details":[{"aspect_id":"1"},{"aspect_id":"4"}]}"

存储值这个选择查询,我将解码JSON格式和检查的foreach该值。

$this->db->select('game_aspect_details'); 
    $this->db->from('share_reviews'); 
    $this->db->where('review_id',6); 
    $query = $this->db->get(); 
    $result = $query->result(); 
    $test = $result[0]->game_aspect_details; 
    $res = json_decode($test); 
    $result_array = array();   
    foreach ($res as $row) 
    {    
     $this->db->select('comments'); 
     $this->db->from('review_ratings'); 
     $this->db->where('game_aspect_id',$row->game_aspect_details); //here i need 
     $query1 = $this->db->get();       to check 1 and 4 
     $resultReviews['comments'] = $query1->result(); 
     $result_array[] = $resultReviews; 

    } 
    print_r($res); 
    exit; 
+6

你有什么问题吗? – 2013-07-29 11:05:49

+0

我需要解码该json格式,我必须得到值1和4 – sangee

回答

2

首先这不看你是使用print_r()得到什么在$rowforeach内。

我希望您需要更换下面的线

$this->db->where('game_aspect_id',$row->game_aspect_details); 

随着下面一行:

$this->db->where('game_aspect_id',$row['aspect_id']); 

$row是一个数组不是对象。

编辑:

foreach ($res as $rows) 
{    
    foreach ($rows as $row) 
    {    
    ........ 
    $this->db->where('game_aspect_id',$row['aspect_id']); //here make change 
    ..... 
    } 
} 
+0

Array([0] => Array([aspect_id] => 1)[1] => Array([aspect_id] => 4)) – sangee

+0

当我print_r($行)内foreach我可以得到这样的 – sangee

+0

@sangee然后使用在你的答案,肯定会工作的代码。 –

1

你正在做的事情有些不对,请参阅本代码中的注释:

... 
//$test = $result[0]->game_aspect_details; // This does not work since $result is not decoded yet 
$res = json_decode($result); // Changed to `$result` instead of `$test` 
$res = $res->game_aspect_details; // Instead pick the `game_aspect_details` here, after the decode 
$result_array = array();   
foreach ($res as $row) 
{    
    $this->db->select('comments'); 
    $this->db->from('review_ratings'); 
    $this->db->where('game_aspect_id',$row['aspect_id']); // Changed to `aspect_id` 
    $query1 = $this->db->get(); 
    $resultReviews['comments'] = $query1->result(); 
    $result_array[] = $resultReviews; 
} 
... 
相关问题