开发者

PHP arrays: cross data

An student here who needs help with the logic of the following:

I have two arrays.

$array1 contains the ids of the places the user has been to.

$array2 contains the ids of the places where the user can obtain one point just by visiting them

Now I need to know if the user has visited any of the places where he/she can get one point so I can grant it to him/her. How can I开发者_如何学Python do that?

Tnaks a lot


Looks like you want to try array_intersect

$intersection = array_intersect($array1, $array2);


count(array_intersect($array1, $array2)) should then give you the number of suych places.


$visited = array('1','2','3','4','5'); // and so on
$awarding = array('3','4','5'); //...

$nr_of_grants = 0;

foreach ($visited as $visit)
{
  if(in_array($visit,$awarding)
  {
    $nr_of_grants++;
  }
}

echo "You've been awarded for".$nr_of_grants."places";  //


You should use array_intersect($array1, $array2). The result would be the ids of places present in both arrays.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜