开发者

Check array for existence

$array = Array
(
    [0] => Array
        (
            [id] => 46
            [title] => sometext
        )

    [1] => Array
        (
            [id] => 47
            [title] => sometext
        )
    [2] => Array
        (
            [id] => 48
            [title] => sometext
        )
    [3] => Array
        (
            [id] => 49
            [title] => sometext
        )
    [4] => Array
        (
            [id] => 50
            [title] => sometext
        )

)

We have an array and a variable:

$variable = 48; //some number

How do we check whether $variable exists in some a开发者_StackOverflow中文版rrays ['id'] inside $array?

Return true or false.


function myCheck($array, $variable)
    foreach($array as $subArray) {
        if($subArray['id'] == $variable) {
            return true;
        }
    }
    return false;
}


Use this function:

function check_array() {
  foreach ($array as $ar) {
    if ($ar['id'] == $variable)
      return true;
  }
  return false;
}


Have you tried array_search? It returns the key value if found, or false if not found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜