2013-07-29 94 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; 
} 
... 
相關問題