table('oa_shijian')." whe"/> table('oa_shijian')." whe">

Home >Backend Development >PHP Tutorial >怎样得到循环的两个邻近的数值的差

怎样得到循环的两个邻近的数值的差

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:13:57997browse

怎样得到循环的两个相邻的数值的差
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    if($i>0){
        echo $arr[$i]-$arr[$i-1];
    }
}
------解决思路----------------------

$last = '';<br />while($row = $db->fetchRow($res))<br />{<br />  //学员总人数<br />  $xy_count = $db->getOne("SELECT COUNT(distinct xy.xy_id) FROM ".<br />  if($last !== '') echo $last - $xy_count; //这里是打印,实际需要是什么你自己定<br />  $last = $xy_count; <br /><br />  //其他代码<br />}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn