如果我理解正确,您从数据库获取一个整数,想知道哪些号码里面存在预定义的数字。您可以将这些整数转换为字符串,而字符串基本上是一个字符数组。因此,首先创建一个数组,其中包含预定义数字中的数字和数量的信息。然后逐个字符地查看数据库编号,并查看它是否存在于预定义数字的数组中。如果是这样,请减少该号码的计数。
的代码看起来是这样的:
$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
*/
感谢我在PHP新,所以我怎么会显示结果..? – 2011-12-15 00:10:11
它取决于哪里。如果你只想看看那里有什么,可以使用var_dump($ variable)或print_r($ variable)。如果您想格式化您必须使用 的foreach($结果为$值){ $位数= $值[0]; $ status = $ value [1];/*在这里做$数字和$状态的东西*/ } – Zefiryn 2011-12-15 07:28:48