簡體   English   中英

在數組中檢查數組是否包含php中的值

[英]check in array if array contains a value in php

我有一個輸入電話號碼489998723(由用戶填寫)。 並形成數據庫,我在數組中得到帶速率的前綴

4899981
4899
4899988
489998
4899987
48999

我將如何執行精確匹配。 示例用戶輸入489998723 ,它應匹配4899987

提前致謝。

建議使用str_pos

$phone = 489998723;

$arr = [
    4899981,
    4899,
    4899988,
    489998,
    4899987,
    48999,
];


rsort($arr);

$result = false;
foreach ($arr as $value) {
    // First match will be string of max length because of sorting
    if (strpos(strval($phone), strval($value)) === 0) {
        $result = $value;
        break;
    }
}

if ($result) {
    print_r ('result: ' . $result . PHP_EOL);
} else {
    print_r ('not found' . PHP_EOL);
}

沙盒代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM