Php 为什么可以';t数组“如果键的代码为”则搜索查找值;gb2312“;?

Php 为什么可以';t数组“如果键的代码为”则搜索查找值;gb2312“;?,php,arrays,Php,Arrays,我的问题是: $state=array("你"=>1); if(array_key_exists("你",$state)) { $result = array_search("你",$state);echo $result; }else { echo "No Exists"; } 我期望得到“1”的结果,但是输出是“不存在”,我不知道为什么程序不能得到键的值你". 将按值搜索给定数组。请尝试以下操作: $state = array("你"=>1); if(array_ke

我的问题是:

$state=array("你"=>1); 
if(array_key_exists("你",$state)) 
{ 
$result = array_search("你",$state);echo $result;
}else
{
echo "No Exists";
}
我期望得到“1”的结果,但是输出是“不存在”,我不知道为什么程序不能得到键的值你".

将按值搜索给定数组。请尝试以下操作:

$state = array("你"=>1); 
if(array_key_exists("你", $state))  { 
  echo $state["你"];
} else {
  echo "No Exists";
}

// => 1

希望下面的功能会有所帮助

<?php
    $array = array('arr1'=>array('find_me'=>'yes you did.'));

    function get_value_by_key($array,$key)
    {
        foreach($array as $k=>$each)
        {
            if($k==$key)
            {
                return $each;
            }

            if(is_array($each))
            {
                if($return = get_value_by_key($each,$key))
                {
                    return $return;
                }
            }

        }
    }
    echo get_value_by_key($array,'find_me');
?>
上面的代码可以正确执行。我不能准确地显示我的问题。现在我粘贴我的代码,有一些问题

<?php
  $file = file("GB2312-HanZiBianMa.txt");  // file encoding type ANSI 
   foreach ($file  as $line_num => $line)
    {
    list($no,$hex,$dec) = preg_split('[\t]',htmlspecialchars($line));;
    $result[$hex] = $dec;
    }
  $result_2 = array_flip($result);
 if(array_key_exists("你",$result_2))   // **can't find the value** 222
    { 
    $state= $result_2["你"];
    echo $state."<br/>";
    }else
    {
    echo "No Exists<br/>";
    }

foreach($result_2 as $k=>$each)   //can get the value using the preg_match  
        {
         if(preg_match('/你/', $k))
            echo $k."\t".$each."<br/>";     
        }   
  ?>



  the format of GB2312-HanZiBianMa.txt is as follows:
    1947    c4e3    你
    1948    c4e4    匿
    1949    c4e5    腻
    1950    c4e6    逆

print\r($state)以查看您的数组密钥是否按预期保存。我的计算机上没有安装该字符集,否则我肯定会查找该字符集,因为
array\u search()
搜索值,而不是键。使用array\u key\u exists而不是array\u search。array\u key\u exists的返回值('你“,$state)为false,我不知道为什么。我知道答案与数组搜索无关,但至少这会解决您的问题。谢谢。我在我的机器上测试了您的代码,它输出“No Exists”,因此数组的返回值为false。但是,如果我使用var\u dump函数,例如var\u dump($state),结果是”你“=>1.我怀疑这是否与编码有关”你“@wangjue调试此类问题确实很困难。正如您所提到的,这可能是一个编码问题。您可以尝试使用
json\u encode($state)
快速查看字符串使用了哪些代码点。对我来说,上面是:
{\u4f60:1}
<?php
  $file = file("GB2312-HanZiBianMa.txt");  // file encoding type ANSI 
   foreach ($file  as $line_num => $line)
    {
    list($no,$hex,$dec) = preg_split('[\t]',htmlspecialchars($line));;
    $result[$hex] = $dec;
    }
  $result_2 = array_flip($result);
 if(array_key_exists("你",$result_2))   // **can't find the value** 222
    { 
    $state= $result_2["你"];
    echo $state."<br/>";
    }else
    {
    echo "No Exists<br/>";
    }

foreach($result_2 as $k=>$each)   //can get the value using the preg_match  
        {
         if(preg_match('/你/', $k))
            echo $k."\t".$each."<br/>";     
        }   
  ?>



  the format of GB2312-HanZiBianMa.txt is as follows:
    1947    c4e3    你
    1948    c4e4    匿
    1949    c4e5    腻
    1950    c4e6    逆
if(array_key_exists("你",$result_2))   // **can't find the value** 222
    { 
    $state= $result_2["你"];
    echo $state."<br/>";
    }else
    {
    echo "No Exists<br/>";
    }