I am trying to convert strings to numbers using an array with keys as a lookup table.
This is the array:
$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);
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"] : "";
The $row["q2_10"]
value is just the different strings taken from MySQL DB.
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.
EDIT 1
After doing a hex dump as suggested, I got the following results
From SQL DB:
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
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
The difference was "E8" vs "C3A8" or "è" from the DB vs "è" from the php string.
So how can I go about ensuring the php string remains "è"?
I ended up changing my table column encoding to
utf8_general_ci
and table collation toutf8_general_ci
. I also added the following lines of code to my php file:It is working now, but I could be doing something not recommended?
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.
Same means here, that the string is the same byte-by-byte. Do a hexdump of the string you get from the database.
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.
Edit: As you compare against the database, the key needs to co-related to the binary sequence in the database:
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.
As you can see it is just
\x
and then the hexcodeE8
. It works in double-quoted strings in PHP.