-1

I just want to know that is there any simplest way to achieve the requirements below?

I Have 2 arrays

ARRAY1=[ "1","5","3","4"]

and

 ARRAY2=[ "1","1","5","5","3","4"]

firstly I want to check is ARRAY2 contains all the values in ARRAY1. i used array_unque and the diff which is working fine.

So now I want to also check is every values in ARRAY1 is repeat morethan one in array2 if it does return true..

How can I achieve this.. Iam a beginner.. can anyone help me with this?

JEJ
  • 814
  • 5
  • 21
  • 1
    Have you tried anything? While we're glad to help when you're stuck, you're still expected to make an effort of your own to try and solve the problem. Start with a simple foreach loop and slowly build your way towards something. – El_Vanja May 28 '21 at 08:30
  • You could use [`array_count_values()`](https://www.php.net/manual/en/function.array-count-values.php) to get the frequence of the values in array 2 and compare with the values of array 1 – Michel May 28 '21 at 08:32
  • This will help you https://stackoverflow.com/questions/1170807/how-to-detect-duplicate-values-in-php-array – pullidea-dev May 28 '21 at 08:33
  • 1
    Side note: you don't need `array_unique` to determine whether it contains all the elements, `array_diff` is enough. – El_Vanja May 28 '21 at 09:52

1 Answers1

0
$array1 = ["1","5","3","4"];
$array2 = ["1","1","5","5","3","3","4","4","7"]; // every value from array 1 is repeated


$array3 = sizeof(array_diff($array1, array_values(array_diff_key($array2, array_unique($array2))) )) > 0 ? false : true;

Link : https://3v4l.org/InJb6

hu7sy
  • 983
  • 1
  • 13
  • 47
  • This will fail is `$array2` has a non-duplicate value that isn't inside `$array1`. The requirement is _"So now I want to also check is every values in ARRAY1 is repeat morethan one in array2"_... – El_Vanja May 28 '21 at 08:51
  • you said in your question that you have done first part of question, please update your question then – hu7sy May 28 '21 at 09:45
  • 1) It's not my question. 2) The first step doesn't affect this. Your code will independently fail when there is a single value that's not present in `$array1`. – El_Vanja May 28 '21 at 09:46
  • 1
    Here's what I'm talking about: https://3v4l.org/qm5hH – El_Vanja May 28 '21 at 09:51
  • It's ok now, but I would include at least a short explanation of how it works, so that any future readers can learn from it. – El_Vanja May 28 '21 at 13:41