2

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 do that?

Tnaks a lot

0

4 Answers 4

7

Looks like you want to try array_intersect

$intersection = array_intersect($array1, $array2);
Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

Well, this was the best answer.
0
$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";  //

2 Comments

He. I was writing this same answer :)
This is less efficient than using array_intersect.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.