如果我理解正確,您從數據庫獲取一個整數,想知道哪些號碼裏面存在預定義的數字。您可以將這些整數轉換爲字符串,而字符串基本上是一個字符數組。因此,首先創建一個數組,其中包含預定義數字中的數字和數量的信息。然後逐個字符地查看數據庫編號,並查看它是否存在於預定義數字的數組中。如果是這樣,請減少該號碼的計數。
的代碼看起來是這樣的:
$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