php 數組問題

來源:互聯網
上載者:User
我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 刪掉不就可以了嗎? 是的,是的,版主仔細,我粗心了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.