PHP:含有特殊字符数组的键未找到 [英] PHP : Array key containing special character not found

查看:138
本文介绍了PHP:含有特殊字符数组的键未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字符串转换为使用数组键作为一个查找表的数字。

I am trying to convert strings to numbers using an array with keys as a lookup table.

这是该数组:

$q2_10_lt = array("Full-time worker" => 1
                , "Part-time worker" => 2
                , "Unemployed, would like to work" => 3
                , "Unable to work (chronically ill/mentally handicapped/physically handicapped)" => 4
                , "Pensioner/retired" => 5
                , "Housewife/husband" => 6
                , "Student at university of college (post-matric)" => 7
                , "High school learner" => 8
                , "Primary school learner" => 9
                , "Child attending pre-school/nursery school/crèche/day-mother" => 10
                , "Child staying at home" => 11
                , "Other" => 12);

有问题的关键是儿童就读pre-学校/幼儿园/托儿所/天娘。使用以下code时,此键未找到:

The problematic key is "Child attending pre-school/nursery school/crèche/day-mother". This key is not found when using the following code:

$person_tempArr[] = $q2_10_lt[$row["q2_10"]] != null ? $q2_10_lt[$row["q2_10"]] : "12";
$person_tempArr[] = $q2_10_lt[$row["q2_10"]] == null ? $row["q2_10"] : "";

$行[q2_10] 值从MySQL数据库只是采取不同的字符串。

The $row["q2_10"] value is just the different strings taken from MySQL DB.

我应该从第一行获得的10号,而是我得到12和完整的字符串不变孩子就读pre-学校/幼儿园/托儿所/天的母亲。

I should get the number 10 from the first line, but instead I get 12 and the complete string unaltered "Child attending pre-school/nursery school/crèche/day-mother".

这必须是与特殊字符è,但我一直没能解决这个问题。任何帮助,请。

This must have something to do with the special character è, but I have not been able to solve it. Any help please.

修改1

做一个十六进制转储的建议后,我得到了以下结果。

After doing a hex dump as suggested, I got the following results

从SQL数据库:

43 68 69 6c 64 20 61 74 74 65 6e 64 69 6e 67 20 70 72 65 2d 73 63 68 6f 6f 6c 2f 6e 75 72 73 65 72 79 20 73 63 68 6f 6f 6c 2f 63 72 e8 63 68 65 2f 64 61 79 2d 6d 6f 74 68 65 72

在PHP字符串:

From string in php:

43 68 69 6c 64 20 61 74 74 65 6e 64 69 6e 67 20 70 72 65 2d 73 63 68 6f 6f 6c 2f 6e 75 72 73 65 72 79 20 73 63 68 6f 6f 6c 2f 63 72 c3 a8 63 68 65 2f 64 61 79 2d 6d 6f 74 68 65 72

所不同的是E8与从PHP字符串C3A8或E,从DB和A。

The difference was "E8" vs "C3A8" or "è" from the DB vs "è" from the php string.

所以,我怎么能去确保PHP字符串仍然是E?

So how can I go about ensuring the php string remains "è"?

推荐答案

您从您的数据库层得到的字符串是不是你作为重点使用相同字符串。要修复它,使用相同的字符串每次。

The string you get from your database layer is not the same string you use as the key. To fix it, use the same string each time.

同样的手段在这里,该字符串是一样的逐字节。做一个<一个href=\"http://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php\">hexdump字符串的你从数据库中获取。

Same means here, that the string is the same byte-by-byte. Do a hexdump of the string you get from the database.

然后输入的关键(或至少是特殊的字母)的二进制字符串。这会让你的code更强大的,因为这将在工作无关编码你保存PHP文件。

Then enter the binary string as key (or at least for the special letter). This will make your code more robust because it will work regardless in which encoding you're saving the PHP file.

修改当你比较对数据库的关键需求,共同与在数据库中的二进制序列:

As you compare against the database, the key needs to co-related to the binary sequence in the database:

        , "Child attending pre-school/nursery school/cr\xE8che/day-mother" => 10
                                                       ^^^^

使用十六进制表示法恩preSS你是不是能够型与您的 UTF-8 的EN codeD PHP文件的字节数。该数据库使用了一些的 ISO-8859-1 的或类似的编码。

Use the hexadecimal notation to express the bytes you are not able to "type" with your UTF-8 encoded PHP file. The database uses some ISO-8859-1 or similar encoding.

正如你可以看到它是 \\ X ,然后十六进制code E8 。它工作在PHP中的双引号字符串。

As you can see it is just \x and then the hexcode E8. It works in double-quoted strings in PHP.

这篇关于PHP:含有特殊字符数组的键未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆