0

I'm having troubles comparing two arrays with array_inersect(), tried lot of suggestion from SO but none worked. So here is my problem:

I have two arrays:

$base_array = array(2,2,2,1);

Array
(
    [0] => 2
    [1] => 2
    [2] => 2
    [3] => 1
)

And another one $risks that prints like this:

Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 1
    [4] => 2
    [5] => 8
    [6] => 6
)

What I want is to see if the same number and type of values from $base_array fits into $risks array. Right now it should return false cause I only have two occurences of number 2 and base array has 3. But if I set $base_array to:

$base_array = array(2,2,1);

This should return true.

5
  • 1
    Show your code. Commented Oct 2, 2016 at 15:26
  • I can't make any code to give me what I need, everything I tried was array_intersect and array_diff but didn't work. Always returning true cause 2 exists in both but no same occurence. Commented Oct 2, 2016 at 15:28
  • Is it supposed to return true when there are two 2's in $base_array? i.e we are checking if there are two 2's and one 1, right? Commented Oct 2, 2016 at 15:28
  • 1
    Add the attempts with array_intersect and array_diff in your question, and in which sense they didn't work. Then we can/will help. Commented Oct 2, 2016 at 15:29
  • @ObjectManipulator exatcly. This is just an example, but yes in this case it should return false cause there are three 2's in $base_array but if we set $base_array = array(2,2,1); it should return True. Commented Oct 2, 2016 at 15:30

2 Answers 2

1

One approach can be counting occurrence of each number then manually comparing them. For this you need two array to keep the occurrence count.

$base_array_count = [];
$risks_count = [];

In your case $base_array_count will look like

$base_array_count[1] = 1
$base_array_count[2] = 2

And the $risks_array_count will look like

$risks_count[1] = 1
$risks_count[2] = 2
$risks_count[3] = 1
$risks_count[4] = 1
$risks_count[6] = 1
$risks_count[8] = 1

Now loop through $base_array and check if each numbers $base_array_count value is same as in $risks_array_count

Complete code after edit:

$base_array = array(1,2,2,7);
$risks_array = array(2,3,4,5,6,7);

$base_array_count = array();
$risks_array_count = [];

for($i = 0; $i < sizeof($base_array); $i++){
    if( !isset($base_array_count[$base_array[$i]] ))
        $base_array_count[$base_array[$i]] = 0;
    $base_array_count[$base_array[$i]]++;
}

for($i = 0; $i < sizeof($risks_array); $i++){
    if( !isset($risks_array_count[$risks_array[$i]] ))
        $risks_array_count[$risks_array[$i]] = 0;
    $risks_array_count[$risks_array[$i]]++;
}

$same = true;

if (sizeof($base_array) <= sizeof($risks_array)) {
    for ($i = 0; $i < sizeof($base_array); $i++) {
        if(!isset($risks_array_count[$base_array[$i]]) || $base_array_count[$base_array[$i]] != $risks_array_count[$base_array[$i]]){
            $same = false;
            break;
        }
    }
} else {
    $same = false;
}
if($same)
    echo "TRUE";
else
    echo "FALSE";
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your suggestion, I tried with array_diff but it didn't worked very well. Can you please explain how should I loop for each $base_array_count and compare?
@rgomez Added the complete code, check if it solves your problem.
almost perfect and sorry for being such meticulous but this will be very important on my code. It works pefectly even tried adding other numbers into base_array like 8 or 4 ($base_array = array(1,2,2,4,8);) and of course If I set 2,2,2 it returns false. But the problem comes if I set for instance $base_array = array(1,2,2,7); I get Undefined offset: 7
@ Risul Islam, after last change according to your edit the Undefined offset Notice goes away but setting $base_array = array(1,2,2,7); should return False and returns True.
@rgomez It shows me False. I don't know why it shows you True. Did you update your code properly? I am changing complete code according to edit try this.
|
1

It can be done by array_diff with some extra check. See here

$base_array = array(1,2,2,7);
$risks_array = array(1,2,2,3,4,5,6);

if(array_diff($base_array, $risks_array)) // false if $base_array has type of elemenes which $risk_array does not contain 
 echo "false"; //
else{
 // $risk_array has all type of elements of $base_array. now check for similar frequency
 $freq_cnt_base = array_count_values($base_array);
 $freq_cnt_risks = array_count_values($risks_array);
 $is_similar = "true";

 foreach($freq_cnt_base as $key => $val){
    if($freq_cnt_risks[$key] !== $freq_cnt_base[$key]){
        $is_similar = "false";
        break;
    }
 }
 echo $is_similar;
}

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.