将特定数字与php中mysql表中连续数字进行比较

Im trying to the following:

I have a specific number and I would like to compare this number to numbers in a row and get count of each number.

I have a sample and its working ok but if a row contains 3 or more same numbers and in the specific number there are only 2 same numbers it will count as 3.

For example if specific number is:

8 8 2 1 2 7 0

and in a row there are following numbers:

8 8 8 2 5 6

Count should be 8=1, 8=1, 8=0; 2=1, 5=0, 6=0

because there are only 2 number 8s in the specific number so the third 8 number should not be counted in the row.

I hope its clear:

here is the link it might be more clear to explain:

http://designer121.comze.com/sample.php

You could use count_chars() to work out the characters in your specific number, and then iterate across the numbers in your row. As you go, decrement the count or that digit from your specific number and only set the flag (=1 part) if the count is greater than zero.

EDIT: Some code:

$number = '8821270';
$rows = '888256';
$count = array();

$digits = count_chars($number, 1); 

foreach (str_split($rows) as $row) {
    if (isset($digits[ord($row)]) && $digits[ord($row)] > 0) {
        $count[] = 1;
        $digits[ord($row)]--;
    } else {
        $count[] = 0;
    }   
}   

var_dump($count);

If I understood correctly you get an integer from db and want to know which numbers inside exist in predefined number. You can convert those integers to string and string is basically an array of characters. So first create an array with information which numbers and in what quantity are in the predefined number. Then go through db number character by character and see if it exist in the array of predefined number. If so decrease the count for that number.

The code could look like this:

$number = 8821270;
$row = 888256;

//create string representation of integer
$n = (string)$number;
//calculate which numbers exist in the $number
$row_n = array();
for($i =0; $i < strlen($n);$i++){
    $char = $n[$i];
    if (isset($row_n[$char])) {
        $row_n[$char]++;
    } 
    else {
        $row_n[$char] = 1;
    }
}
/*
  at this point $row_n is an array where the key is a digit from $number 
  and the value is the quantity
*/

//create string representation of fetched number
$s = (string)$row;
for($i =0; $i < strlen($s);$i++){
    $char = $s[$i];

    if (isset($row_n[$char]) && $row_n[$char] > 0) {
        $result[] = array($char, 1);
        $row_n[$char]--; //decrease count for that number
    }
    else {
        $result[] = array($char, 1);
    }
}

/*
  Here result is an array with information on each separate number
  Right now it looks like this:
  array 
  0 => array
       0 => digit
       1 => status
  1 => array
       0 => digit
       1 => status

  Modify $result[] = array($char, 1); to make it look whatever you want

*/

You can use array_intersect_assoc, but you have to convert your numbers to arrays first:

$numberOne = '8821270'; 
for($i=0;$i< strlen($numberOne);$i++){
    $numberOneArray[] = $numberOne[$i];
}

$numberTwo = '888256'; 
for($i=0;$i< strlen($numberTwo);$i++){
    $numberTwoArray[] = $numberTwo[$i];
}

$result = array_intersect_assoc($numberOneArray, $numberTwoArray);
// display the result
print_r(array($numberOneArray, $numberTwoArray,$result));

As the function preserves keys, you can now look for consecutive keys, starting from 0