-3

I have two integers for example "12345" and "98754", they have a count of 2 matching numbers namely 4 and 5, the order doesnt matter.

Now: How do I check something like that in PHP?

  • Check what? what do you mean by _they have a count of 2 matching numbers_ – Spoody Apr 08 '18 at 18:43
  • Both of them have a 4 and a 5 - that's a total of 2 matching numbers – BurningSimon Apr 08 '18 at 18:47
  • Not sure if there's a stringy way, but you could put all numbers in an array and check each value that way – James Apr 08 '18 at 18:49
  • Possible duplicate of [How to check if two strings contain the same letters?](https://stackoverflow.com/questions/6807864/how-to-check-if-two-strings-contain-the-same-letters) – Spoody Apr 08 '18 at 18:49
  • 1
    BTW, if this is for coursework (apologies if not) then you really should try to work it out first and ask when stuck. Otherwise you'll not learn to code, just how to ask how to code ;) – James Apr 08 '18 at 18:51
  • yeah true James, but I don't have any idea how to start :D No this is not for coursework, I am programming something for fun :) – BurningSimon Apr 08 '18 at 21:19

3 Answers3

4

You can split the inputs to arrays and use array_intersect to find matching numbers.

$a = 12345;
$b = 98754;

//Create arrays of the numbers
$a = str_split($a);
$b = str_split($b);

// Find matching numbers
$matching = array_intersect($a, $b);
Var_dump($matching);
// Output: 4,5
Echo count($matching);
// Output: 2

https://3v4l.org/8tS3q

Andreas
  • 23,610
  • 6
  • 30
  • 62
1
  1. Convert the numbers in to strings
  2. create a loop from 0-9 to check for the appearance of a number in both strings using strstr() or similar
  3. store the number in an array if it appears in both

Edit: Code-centric solution:

$a = 1231534;
$b = 89058430;


    $matches = compare( $a, $b );   
    print count($matches);


    function compare ( $a, $b ) {
        $str_a = (string) $a;
        $str_b = (string) $b;
        $matches = [];
        for($i=0;$i<=9;$i++) {
            if (strstr($str_a, (string)$i) && strstr($str_b,(string)$i)) $matches[] = $i;
        }

        return $matches;
    }
Matthew Knight
  • 631
  • 5
  • 13
  • 1
    You need to provide a more *codey* answer for this really. Conceptual answers on Stack Overflow are not really much use to anyone. Also, re your `2`, what if there are more than 9 numbers? What number do they check for an appearance for? – James Apr 08 '18 at 18:53
  • sorry, i was trying to help the original poster figure out a solution, rather than providing the code. re. #2, there are only 0-9 numeric digits, so it only needs to check for those characters in both strings. – Matthew Knight Apr 08 '18 at 19:04
  • @BurningSimon keep in mind this solution is the only solution here that does not show if one number is duplicated twice (look at Progrocks example numbers). If you have the 4,4,5 you can delete the duplicate with array_unique(), but if you don't have the duplicate you can't get them either. – Andreas Apr 09 '18 at 06:58
0

Added an example here that counts digits that occur in both numbers. If multiple digits occur in both, these are included:

<?php
function digits_in_both($x, $y)
{
    $in_both = [];
    $split_y = str_split($y);
    foreach(str_split($x) as $n) {
        $key = array_search($n, $split_y);
        if($key !== false) {
            $in_both[] = $n;
            unset($split_y[$key]);
        }
    }

    return $in_both;
}

$in_both = digits_in_both(123445, 4456);
var_export($in_both);
var_dump(count($in_both));

Output:

array (
  0 => '4',
  1 => '4',
  2 => '5',
)int(3)

Contrary to what you expect with array_intersect, order matters as demonstrated here:

var_export(array_intersect(str_split('024688'), str_split('248')));
var_export(array_intersect(str_split('248'), str_split('024688')));

Output:

array (
    1 => '2',
    2 => '4',
    4 => '8',
    5 => '8',
  )array (
  0 => '2',
  1 => '4',
  2 => '8',
)
Progrock
  • 7,373
  • 1
  • 19
  • 25