检查多维关联数组中的值

时间:2014-01-07 15:09:42

标签: php multidimensional-array

我有一个多维关联数组,如下所示:

object(TokenManager)#4 (2) {
  ["tokens"]=>
  array(3) {
    ["value1"]=>
    array(11) {
      ["201401071655"]=>
      string(40) "58abbc160c02fa16a6084ee4806aa52c871367d9"
      ["201401071656"]=>
      string(40) "2143695c36ac8410cf1c27bc1bdc7d16d77d1b37"
      ["201401071657"]=>
      string(40) "17a3b5cb25f6f8e9ddd5807fe019d7c3aba7e69d"
      ["201401071658"]=>
      string(40) "ba93fce5d47b6a4118fbe9d0d6783c72f556f75d"
      ["201401071659"]=>
      string(40) "f3aa74d02db4476036cacb26eb4df590ee8f050e"
      ["201401071700"]=>
      string(40) "4ada4f540dd39da2410b9c0c1b28727e135e29da"
      ["201401071701"]=>
      string(40) "4401ae2881a719beb6b517cdd7a9ca9638c58f50"
      ["201401071702"]=>
      string(40) "35beddba873896b5fae72f6eb018e09993a9fce9"
      ["201401071703"]=>
      string(40) "68dfa83105289fcb7e6661e3a93124b3dabaa8dc"
      ["201401071704"]=>
      string(40) "8064fb3edc4ee2f5ec8384fb8e9cbd067f6c3c3c"
      ["201401071705"]=>
      string(40) "cc49b55ec2cdb70aa8b08b179879040786771d52"
    }
    ["value2"]=>
    array(11) {
      ["201401071655"]=>
      string(40) "05b5128b6475b5657da1a17502f49e2c216a0ae6"
      ["201401071656"]=>
      string(40) "7fca1b15c0f71b1e88638a766598b18e881416d3"
      ["201401071657"]=>
      string(40) "89e36412009bc761857391912c8f71407e27cea0"
      ["201401071658"]=>
      string(40) "f0af00aa784faaa1ba3e218dc414facb544fbd4b"
      ["201401071659"]=>
      string(40) "781973964d1241eeac087b6441659293780c7a76"
      ["201401071700"]=>
      string(40) "5fec09016574765df29cb22f1828b765f0b5d6bf"
      ["201401071701"]=>
      string(40) "fe2ab03cbbb98e8eacd84b91643891597cc5f38c"
      ["201401071702"]=>
      string(40) "4650fe079823cac7f2fb14451c6b68402918b3d9"
      ["201401071703"]=>
      string(40) "eecb43e4fa92fbde18ac28a605832f20ab340dee"
      ["201401071704"]=>
      string(40) "af040ddead7c227cea9a55f87c4350313b4dae2a"
      ["201401071705"]=>
      string(40) "14704756efad3891fb8897687844b385b81bb894"
    }
    ["value3"]=>
    array(11) {
      ["201401071655"]=>
      string(40) "252ab80a48b1a9059ffebf3546b68bf7071eb9c0"
      ["201401071656"]=>
      string(40) "d839b3b716616639ab8420a7f9e80cd8dd15fefd"
      ["201401071657"]=>
      string(40) "30352843288999cd5189a546d2105173362c4697"
      ["201401071658"]=>
      string(40) "ecff916f80dd40d16eb4c933a38496f9a7f4c520"
      ["201401071659"]=>
      string(40) "cd2232ad4879687d50c6519794c91e8a041e0677"
      ["201401071700"]=>
      string(40) "b95c007c3e64e9bc4e41c3606269708a08f36ff2"
      ["201401071701"]=>
      string(40) "0a2d0beabd1f0007081986627d220947cc47bcb6"
      ["201401071702"]=>
      string(40) "8edfabea4e87fa842b260ed6a24cb90f786360e7"
      ["201401071703"]=>
      string(40) "1c72540b919a964167778e960036d88d7aae518a"
      ["201401071704"]=>
      string(40) "6e9d88d2a46984f7a5bf9f15b22b2c176583b420"
      ["201401071705"]=>
      string(40) "53fa3586338fc947d7e02818467e7807b22183ee"
    }

我正在尝试检查其中一个子数组中是否存在给定值,如果是,则返回此类数组的键(value1 | value2 | value3)

   public function checkTokenAndGetChannel($token){
        $ans = null;

        foreach($this->tokens as $chanel=>$tokenArr){
            foreach($tokenArr as $vToken){
                if(strcmp($vToken,$token)==0){
                    return $chanel;
                }
            }


//            if(array_key_exists($chanel, $token)){
//                $ans = $chanel;
//                break;
//            }
        }

        return $ans;
    }

我做错了什么?

1 个答案:

答案 0 :(得分:2)

public function checkTokenAndGetChannel($token){

        foreach($this->tokens as $chanel=>$tokenArr){
            if (in_array($token, $tokenArr)){
                return $chanel;
            }
        }
        return false;
    }