PHP搭配两个数组,并给予匹配颜色 [英] PHP match two arrays and give match a color
本文介绍了PHP搭配两个数组,并给予匹配颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个阵列功能。
一个阵列名为 $用户
和一个阵列名为冠军
两者都具有阵列中10个号码。
我怎样才能找出 $用户
的 $赢家匹配多少个号码
?
并为每场比赛,你包从 $用户
的spesific数目与绿色跨度颜色。
我已经写了code,但它不会正确地匹配它们的过程中,造成数可以随时随地在阵列中,而不是在同一个地方。
目前的code:
函数calculateMatches($用户,$获得者){
排序($用户);
排序($冠军); $ X = 0;
$匹配= 0; 的foreach($冠军为$号){ 如果($数== $用户[$ X]){
$用户[$ X] =<跨度风格= \\颜色:绿色; \\>中$用户[$ X]< / SPAN>中;
$匹配++;
} $ X ++;} 返回数组('匹配'=> $比赛,'用户'= GT; $用户,'赢家'=> $冠军);
}
解决方案
您可以使用 in_array
与 array_search
。
如果(in_array($号码,$用户){
$ X = array_search($号码,$用户,真实);
$用户[$ X] =<跨度风格= \\颜色:绿色; \\>中$用户[$ X]< / SPAN>中;
$匹配++;
}
I have a function with two arrays.
One array called $user
and one array called winner
both have 10 numbers in the array.
How can i find out how many numbers in $user
that matches the $winner
?
and for each match, you wrap the spesific number from $user
with a span color of green.
I have written a code , but it wont match them correctly of course, cause the number may be anywhere in the array, not on the same spot.
current code:
function calculateMatches($user,$winner) {
sort($user);
sort($winner);
$x=0;
$match = 0;
foreach($winner as $number) {
if ($number == $user[$x]) {
$user[$x] = "<span style=\"color:green;\">".$user[$x]."</span>";
$match++;
}
$x++;}
return array('match' => $match,'user' => $user, 'winner' => $winner);
}
解决方案
You can use in_array
with array_search
.
if(in_array($number, $user) {
$x = array_search($number, $user, true);
$user[$x] = "<span style=\"color:green;\">".$user[$x]."</span>";
$match++;
}
这篇关于PHP搭配两个数组,并给予匹配颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文