怎樣得到迴圈的兩個鄰近的數值的差

來源:互聯網
上載者:User
怎樣得到迴圈的兩個相鄰的數值的差
function get_shijian_list($school_id,$parent_id)
{
$sql = "select shijian_id,name,type from ".$fdyu->table('oa_shijian')." where parent_id=".$parent_id." and school_id=".$school_id." order by shijian_id asc";
$res = $db->query($sql);
$arr = array();
if($res)
{
while($row = $db->fetchRow($res))
{
//學員總人數
$xy_count = $db->getOne("SELECT COUNT(distinct xy.xy_id) FROM ".
$fdyu->table('oa_xueyuan')." as xy left join ".
$fdyu->table('oa_banji')." as bj on xy.cur_banji_id=bj.banji_id
(bj.shijian_1=".$row['shijian_id']." or bj.shijian_2=".$row['shijian_id']." or bj.shijian_3=".$row['shijian_id'].")
");

$sj_id= $row['shijian_id'];
$arr[$sj_id]['sj_id']= $sj_id;
$arr[$sj_id]['xy_count_s']= $xy_count;
}
}
return $arr;


請問在迴圈中的$xy_count,怎樣計算兩個相鄰的差,比如,得到的$xy_count分別是100、80、50,怎樣得到
100-80=20
80-50-30
------解決思路----------------------
簡單點可以用for

$arr = array(1,2,3,4);
for($i=0,$len=count($arr); $i<$len; $i++){
if($i>0){
echo $arr[$i]-$arr[$i-1];
}
}
------解決思路----------------------
$last = '';
while($row = $db->fetchRow($res))
{
//學員總人數
$xy_count = $db->getOne("SELECT COUNT(distinct xy.xy_id) FROM ".
if($last !== '') echo $last - $xy_count; //這裡是列印,實際需要是什麼你自己定
$last = $xy_count;

//其他代碼
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.