Home >Backend Development >PHP Tutorial >能否根据下面的两个数组来判断趋势?

能否根据下面的两个数组来判断趋势?

WBOY
WBOYOriginal
2016-06-23 14:23:561217browse

php

                     $old_rank = array(			"1"=>array('uid'=>132,'use_score'=>61570),			"2"=>array('uid'=>121,'use_score'=>50932),			"3"=>array('uid'=>145,'use_score'=>46789),			"4"=>array('uid'=>100,'use_score'=>39089),			"5"=>array('uid'=>167,'use_score'=>29089)		);		$new_rank = array(			"1"=>array('uid'=>132,'use_score'=>61570),			"2"=>array('uid'=>145,'use_score'=>51932),			"3"=>array('uid'=>121,'use_score'=>50932),			"4"=>array('uid'=>167,'use_score'=>40089),			"5"=>array('uid'=>100,'use_score'=>39089)		);

这样的两个数组,怎么进行比对得出排名的趋势,比如用户145排名上升一位了,121排名下降一位了,132排名保持不变。

回复讨论(解决方案)

$old_rank = array(  "1"=>array('uid'=>132,'use_score'=>61570),  "2"=>array('uid'=>121,'use_score'=>50932),  "3"=>array('uid'=>145,'use_score'=>46789),  "4"=>array('uid'=>100,'use_score'=>39089),  "5"=>array('uid'=>167,'use_score'=>29089));$new_rank = array(  "1"=>array('uid'=>132,'use_score'=>61570),  "2"=>array('uid'=>145,'use_score'=>51932),  "3"=>array('uid'=>121,'use_score'=>50932),  "4"=>array('uid'=>167,'use_score'=>40089),  "5"=>array('uid'=>100,'use_score'=>39089));foreach($old_rank as $k=>$v) $old[$v['uid']] = $k;foreach($new_rank as $k=>$v) $new[$v['uid']] = $k;foreach($new as $k=>$v)  echo "$k 排名 $v " . ($v - $old[$k]) . '<br>';
132 排名 1 0
145 排名 2 -1
121 排名 3 1
167 排名 4 -1
100 排名 5 1

$old_rank = array(  "1"=>array('uid'=>132,'use_score'=>61570),  "2"=>array('uid'=>121,'use_score'=>50932),  "3"=>array('uid'=>145,'use_score'=>46789),  "4"=>array('uid'=>100,'use_score'=>39089),  "5"=>array('uid'=>167,'use_score'=>29089));$new_rank = array(  "1"=>array('uid'=>132,'use_score'=>61570),  "2"=>array('uid'=>145,'use_score'=>51932),  "3"=>array('uid'=>121,'use_score'=>50932),  "4"=>array('uid'=>167,'use_score'=>40089),  "5"=>array('uid'=>100,'use_score'=>39089));foreach($old_rank as $k=>$v) $old[$v['uid']] = $k;foreach($new_rank as $k=>$v) $new[$v['uid']] = $k;foreach($new as $k=>$v)  echo "$k 排名 $v " . ($v - $old[$k]) . '<br>';
132 排名 1 0
145 排名 2 -1
121 排名 3 1
167 排名 4 -1
100 排名 5 1
楼上正解

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
Previous article:thinkphp关联查询,name属性冲突Next article:过滤问题