我dump了一個數組,顯示如下:
array(9) { [0] => array(6) { ["que_content"] => string(11) "11111111111" ["que_id"] => string(1) "1" ["ops_qid"] => string(1) "1" ["ops_contentoid"] => string(1) "A" ["ops_content"] => string(14) "aaaaaaa1111111" ["ops_count"] => string(1) "3" } [1] => array(6) { ["que_content"] => string(11) "11111111111" ["que_id"] => string(1) "1" ["ops_qid"] => string(1) "1" ["ops_contentoid"] => string(1) "B" ["ops_content"] => string(10) "bbbbbbbbbb" ["ops_count"] => string(1) "1" } [2] => array(6) { ["que_content"] => string(11) "11111111111" ["que_id"] => string(1) "1" ["ops_qid"] => string(1) "1" ["ops_contentoid"] => string(1) "C" ["ops_content"] => string(8) "cccccccc" ["ops_count"] => string(1) "1" } [3] => array(6) { ["que_content"] => string(9) "222222222" ["que_id"] => string(1) "2" ["ops_qid"] => string(1) "2" ["ops_contentoid"] => string(1) "D" ["ops_content"] => string(9) "ddddddddd" ["ops_count"] => string(1) "3" } [4] => array(6) { ["que_content"] => string(9) "222222222" ["que_id"] => string(1) "2" ["ops_qid"] => string(1) "2" ["ops_contentoid"] => string(2) "EF" ["ops_content"] => string(12) "efe2efef2fef" ["ops_count"] => string(1) "1" } [5] => array(6) { ["que_content"] => string(9) "222222222" ["que_id"] => string(1) "2" ["ops_qid"] => string(1) "2" ["ops_contentoid"] => string(1) "F" ["ops_content"] => string(14) "fff32fffffffff" ["ops_count"] => string(1) "1" } [6] => array(6) { ["que_content"] => string(9) "333333333" ["que_id"] => string(1) "3" ["ops_qid"] => string(1) "3" ["ops_contentoid"] => string(2) "GG" ["ops_content"] => string(13) "ggg323ggggggg" ["ops_count"] => string(1) "1" } [7] => array(6) { ["que_content"] => string(9) "333333333" ["que_id"] => string(1) "3" ["ops_qid"] => string(1) "3" ["ops_contentoid"] => string(1) "H" ["ops_content"] => string(10) "hhh5h5h5h5" ["ops_count"] => string(1) "2" } [8] => array(6) { ["que_content"] => string(9) "333333333" ["que_id"] => string(1) "3" ["ops_qid"] => string(1) "3" ["ops_contentoid"] => string(1) "J" ["ops_content"] => string(8) "j8j8j8j8" ["ops_count"] => string(1) "2" }}
我想輸出的時候這樣子輸出,應該要怎樣做?
問題:11111111111選項:A 內容:aaaaaaa1111111選項:B 內容:bbbbbbbbbb選項:C 內容:cccccccc問題:222222222選項:D 內容:ddddddddd選項:EF 內容:efe2efef2fef選項:F 內容:fff32fffffffff問題:333333333選項:GG 內容:ggg323ggggggg選項:H 內容:hhh5h5h5h5選項:J 內容:j8j8j8j8
回複討論(解決方案)
剛剛弄出來你看看:
foreach($a as $v){ if($qid!=$v['ops_qid']){echo '問題:'.$v['que_content']."
";$qid=$v['ops_qid']; }else{ echo '選項:'.$v['ops_contentoid'].' 內容'.$v['ops_content']."
"; }}
貌似要稍加改動:
代碼如下:
foreach($a as $v){ if($qid!=$v['ops_qid']){ echo '問題:'.$v['que_content']."
"; echo '選項:'.$v['ops_contentoid'].' 內容'.$v['ops_content']."
"; $qid=$v['ops_qid']; }else{ echo '選項:'.$v['ops_contentoid'].' 內容'.$v['ops_content']."
"; }}
if($qid!=$v['ops_qid'])
$qid是指什麼啊?
$qid 是一個臨時變數,用於存放上一輪的 ops_qid。這樣就可以檢查到是否是新問題了
to nowphp
為什麼要改成 #2 的樣子,將 #1 的 else 刪掉不就可以了嗎?
$qid 是一個臨時變數,用於存放上一輪的 ops_qid。這樣就可以檢查到是否是新問題了
to nowphp
為什麼要改成 #2 的樣子,將 #1 的 else 刪掉不就可以了嗎? 是的,是的,版主仔細,我粗心了。