How to compare and count number of pairs in arr1 and arr2?

arr1 = {2,5,8}
arr2 = {2,4,9}
Expected output:
Element 1: number of pair count is:2
Element 2: number of pair count is:4
Element 3: number of pair count is:8

Hi mariappansiddan welcome to the forum.

Sorry, but I’m not understanding your question.

What do you mean by “element” and why are those counts expected?

Sir

Basically I am trying to compare elements of two array.

Thatis element[0] of arr1 will be compared with element[0] of arr2.

Scenario 1:
If the values are same I need to count number of possible pairs in element and echo the value in PHP.
Example: arr1 = {2} and arr2 = {2} then it is considered as 2 pairs.

Scenario 2:
If the values are not same I need to count number of possible pairs in that element and echo the value in PHP.
Example: arr1 = {4} and arr2 = {5} then it is considered as 4 pairs, as arr1 value is 4 and arr2 vaule is 5.
Max possible pair count is 4 on each array.

It is one on one element comparison. Means arr1 → 0th element needs to be compared to arr2 → 0th element.
and arr1 → 1st element needs be compared to arr2 → 1st element and so on…

Thank you for your reply.

How far have you got in coding this, @mariappansiddan?

I’ve had some sleep and some coffee and I think I understand what you’re wanting to do. What confused me is the use of “pairs” when you mean what I would have called values. That is, each individual value represents a number of pairs,

* BTW, the variable name suggests it’s an arrray, but the curly braces is the syntax for objects, not arrays.

Please correct me if this is incorrect.

  • There are two collections (arrays / objects) that contain numerical values.
  • The two have the same number of items
  • When comparing the two in sequential order, the lower of the two values is the desired value

If the above is correct, it should be fairly easy to do. There is more than way to do it.

Please post the code you have now and describe the problem(s) you are having with it.

Sir

You have understood the scenario well, kindly help with an algorithm to achieve this in PHP. Thank you

@mariappansiddan I looked at the manual and think this is where you found the confusing “pair” term:

Php Online Manual

Specifying with array() ¶

An array can be created using the array() language construct.
It takes any number of comma-separated key => value pairs as arguments.


array
(
key => value,
key2 => value2,
key3 => value3,
…
…
);

The comma after the last array element is optional and can be omitted.
This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ).
For multi-line arrays on the other hand the trailing comma is commonly used,
as it allows easier addition of new elements at the end.

Try this for starters:

<?php 
  declare(strict_types=1);
  ini_set('display_errors', 'true');
  error_reporting(-1);


  $arr1 = array(2, 5, 8);
  $arr2 = array(2, 4, 9);

  echo '<dl>';
    foreach( $arr1 as $key1 => $item1 ):
      
      echo '<dt>$arr1[ $key1 ] ==>  &nbsp; ';
        echo  $item1; 
      echo '</dt>';

      // NEST $arr2
        foreach( $arr2 as $key2 => $item2 ):
          echo '<dd> $arr2[ $key2 ] ==> &nbsp; ';
            echo $item2;
          echo '</dd>';  
        endforeach;  
      echo '<dd> &nbsp; </dd>';
    endforeach;  
  echo '</dl>';

**Output:** ``` $arr1[ $key1 ] ==> 2 $arr2[ $key2 ] ==> 2 $arr2[ $key2 ] ==> 4 $arr2[ $key2 ] ==> 9

$arr1[ $key1 ] ==> 5
$arr2[ $key2 ] ==> 2
$arr2[ $key2 ] ==> 4
$arr2[ $key2 ] ==> 9

$arr1[ $key1 ] ==> 8
$arr2[ $key2 ] ==> 2
$arr2[ $key2 ] ==> 4
$arr2[ $key2 ] ==> 9

What about something like this (untested) function:

function testary($ar1, $ar2) { 
  $counter = 0;
  while ($counter < min(count($ar1), count($ar2)) { 
    echo min($ar1[$counter], $ar2[counter]);
    $counter++;
    }
  }

Would only work with “normal” arrays, though, and assumes no gaps in the array.

Assumptions can result in bugs.

I think many programmers - at least I do - have a tendency to want to jump right to coding. (the fun stuff)

For example, the two example data sets in the opening post are all positive integers and of equal length. But what if the data sets were like:

2, 0,    5,  , 7, -3, 8, "A", 3, "", 9
4, 3, 1.72, 13 

Even when the source data is not corrupt, the more complex the code is, the more chance there is that something wrong could happen.

It is important to consider what should happen in the event of any FAIL. Often it is OK to fail silently, but often it is not.

1 Like

It does reinforce the need to set up repeatable tests, both for expected outcomes and for those situations where the code is asked to deal with the unexpected.

^ Agree. Anyway, @mariappansiddan please take no offense, but I have the impression you are new to writing code. I think what could work here is to “walk” through a “callback”.

IMHO, a good start would be to write code that goes through the data one-by-one and displays each value.

Can you come up with that much?

That is a fair point, and my note on the end was just to highlight that I’d considered that it was perhaps a little too simple. But for the information contained in the OPs question, it would work.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.