簡體   English   中英

如何在mutli dimensioanl數組中搜索值並在找到后返回true,否則返回false

[英]how to search for a value inside mutli dimensioanl array and return true on found else return false

我在這里幾乎沒有什么問題,我正在嘗試在數組內查找特定的IP,因此, 如果此IP退出,我想返回true,否則我想返回false

但是,我已經嘗試了很多方法,但是沒有用

我只需要你的幫助,謝謝。

function user_ip_exists($ip)
{
    // ** Get Connection
    open_handler();         

    $sql = sprintf(" SELECT `ip` FROM `answ` ");

    $query = mysql_query($sql);

    $database_ip = array();

    for ($counter=0; $row = mysql_fetch_assoc($query); $counter++) { 

        foreach ($row as $key => $value) {
            $database_ip[] = $value;
        };
    }

    var_dump($database_ip);

}
// this the above var_dump

array (size=9)
  0 => string '' (length=0)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string '' (length=0)
  4 => string '' (length=0)
  5 => string ',127.0.0.1,127.0.0.1' (length=20) // ip address is separated with ' , '
  6 => string '' (length=0)
  7 => string '' (length=0)
  8 => string '' (length=0)

不建議使用mysql_*函數,而必須使用mysqli或任何其他接口(如PDO)

為了搜索數組,您可以對其進行另一個循環:

foreach($database_ip as $ip)
{
  $parts = explode(",", $ip);

  if(in_array($parts, $your_ip)) return true;
}

暫無
暫無

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

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