Board logo

標題: [操作疑難] inner foreach loop [打印本頁]

作者: carlkyo    時間: 2017-3-17 17:40     標題: inner foreach loop

本帖最後由 carlkyo 於 2017-3-17 17:43 編輯

請問一下各位CHING
我想係$district->name加埋序號
用普通i++行唔到
many thanks
  1.         $xmldata=simplexml_load_file("https://opendata.clp.com.hk/GetChargingSectionXML.aspx?lang=tc") or die("Error: Cannot create object");
  2.         foreach($xmldata->areaList->area as $xml){  
  3.                 echo '<hr/>';
  4.                 echo $xml->name.'<br/>';
  5.                 echo '<hr/>';
  6.                 foreach($xml->districtList->district as $district){
  7.                 echo $district->name.'<br/>';
  8.                 }
  9.         }
複製代碼

作者: 7h1r733n    時間: 2017-3-17 17:48

  1. echo @++$t.". ".$district->name.'<br/>';
複製代碼

作者: 亞熙    時間: 2017-3-17 18:05

  1. foreach($xmldata->areaList->area as $xml){  
  2.                 echo '<hr/>';
  3.                 echo $xml->name.'<br/>';
  4.                 echo '<hr/>';
  5.                 foreach($xml->districtList->district as $key=>$district){
  6.                 echo ($key+1) + ". " + $district->name.'<br/>';
  7.                 }
  8.         }
複製代碼
但咁寫就會由每一個區由1開始count落去

咁樣又得唔得
  1. $i = 1;
  2. foreach($xmldata->areaList->area as $xml){  
  3.                 echo '<hr/>';
  4.                 echo $xml->name.'<br/>';
  5.                 echo '<hr/>';
  6.                 foreach($xml->districtList->district as $district){
  7.                 echo $i + ". " +$district->name.'<br/>';
  8.                 $i++;
  9.                 }
  10.         }
複製代碼
再唔係或者咁
作者: carlkyo    時間: 2017-3-17 22:11

多謝兩位ching指教
many thanks





歡迎光臨 電腦領域 HKEPC Hardware (https://www.hkepc.com/forum/) Powered by Discuz! 7.2