怎样得到循环的两个邻近的数值的差
程序员文章站
2024-01-01 13:39:10
...
怎样得到循环的两个相邻的数值的差
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];
}
}
------解决思路----------------------
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 = '';
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;
//其他代码
}
相关文章
相关视频