Reputation: 2448
I have a database where encoding is UTF-8 for multilingual purpose. I this that everything in my app is in UTF-8.
Though I have a problem with the array_key_exists function.
SELECT name, value from TABLE
$hashmap[ $row['name'] ] = $row['value']
But when the name in the DB contains accents like 'é', the following returns false :
$this->db->select('name');
$this->db->select('value');
$this->db->from('table');
$q = $this->db->get();
$res = $q->result_array();
foreach ($res as $value) {
$hashmap[$value['name']] = $value['value'];
}
$key = 'name é'; // an ord here returns 233
array_key_exists($key, $hashmap)
I dont know how to go further with that, did you encountered the problem ?
I have a performance requirement.
Thanks for your help.
Upvotes: 4
Views: 4283
Reputation: 2448
@Berry
Indeed, when I build the hashmap, debugging I get this :
$res = getDATABASEVALUES();
//res contains 20 rows, the number 11 has accent
$res[11]['name'] => returns 'name é'
ord($res[11]['name'][5]) => returns 195 and not 233
Upvotes: 0
Reputation: 28891
Code:
<?php
$foo = array('namé' => 1);
var_dump($foo);
var_dump(array_key_exists('namé', $foo));
var_dump(isset($foo['namé']));
Output:
array(1) {
["namé"]=>
int(1)
}
bool(true)
bool(true)
PHP version:
PHP 5.3.3 (cli) (built: Sep 13 2011 11:17:59)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
It seems to work okay for me in PHP 5.3.3.
Upvotes: 0
Reputation: 522005
The array key is encoded in UTF-8 if it indeed comes as UTF-8 string from the database. Apparently your source code file is not encoded in UTF-8, I'd guess it's encoded in Latin-1. A comparison between a UTF-8 byte sequence and a Latin-1 byte sequence is therefore unsuccessful. Save you source code files in UTF-8 and it should work (consult your text editor).
Upvotes: 4
Reputation: 18859
But when the name in the DB contains accents like 'é', the following returns false : array_key_exists('namé', $hashmap)
Alas, up till now, string in PHP aren't UTF-8 encoded. I believe you'll have to encode them yourself:
<?php
array_key_exists(utf8_encode('namé'), $hashmap);
Upvotes: 1