我想使用數組此查詢它會像使一個新的數據結構:陣列數據結構的PHP問題
將與查詢和代碼示例
query http://img85.imageshack.us/img85/170/query.png 我想使一個新的啓動使用陣列對這個查詢的數據結構將是這樣的:
//this is for the first headline
[46] => Array
(
[headline_name] => headline 1
[score] => 50
[questions] => Array
(
[0] => Array
(
[question_id] => 136
[question_name] => question 3 , headline 1
[choice_0] => 0
[choice_1] => 0
[choice_2] => 0
[choice_3] => 0
[choice_4] => 0
) ,
[1] => Array
(
....
)
)
)
代碼我寫想要實現這一結果是:
foreach ($result as $row) {
$data[$row->headline_id]= array(
'headline_name' => $row->headline_name ,
'score' => $row->score,
);
$data[$row->headline_id]['questions'][] = array (
'question_id'=>$row->question_id ,
'question_name'=>$row->question_name ,
'choice_0' => $row->choice_0 ,
'choice_1' => $row->choice_1 ,
'choice_2' => $row->choice_2 ,
'choice_3' => $row->choice_3 ,
'choice_4' => $row->choice_4
);
}
與它的問題,它只能顯示在每個標題的最後一個問題,每個問題數組的數組不會被添加它們彼此覆蓋
,如果我想獲得它的工作循環我必須刪除我必須刪除得分和headline_name線,代碼將是這樣
foreach ($result as $row) {
$data[$row->headline_id]['questions'][] = array (
'question_id'=>$row->question_id ,
'question_name'=>$row->question_name ,
'choice_0' => $row->choice_0 ,
'choice_1' => $row->choice_1 ,
'choice_2' => $row->choice_2 ,
'choice_3' => $row->choice_3 ,
'choice_4' => $row->choice_4
);
}
我想的溶液與寫入所述陣列中的headline_name和得分解決它
我真的不知道你需要[「問題」]後的括號[];會更像 $ data [$ row-> headline_id] ['questions'] =數組( 但如果你是循環行,我沒有看到你的行中的數組,因此我沒有看到你想循環 – fabjoa 2010-08-08 12:03:21