PHP: special character as key in array
Change the request method to POST
:
$.ajax({
type:'POST',
url: 'http://192.168.10.24/php/sig.php',
data: {selectedLegalform: selection},
...
In PHP:
$data = $_POST['selectedLegalform'];
If you send the string GmbH & Co.KG
via GET
it becomes GmbH%20%26%20Co%2EKG
. That should be the problem.
Unable to access array key when key name have special character
From @Jeto:
Your key probably contains HTML entities. And since you're probably dumping them on a webpage, you're not seeing them. Check the source of the page. Pretty sure you'll see Shipper's choice
associative array key changes in php for special characters
You can access these attributes directly, e.g.
echo $elem["Reference"];
echo $elem["Key"];
As @Mark Baker pointed in the right direction, the actual magic of SimpleXML
happens behind the scenes. As @deceze mentionned, this is specific to SimpleXML.
You can even loop over them:
foreach($xml->foo[0]->attributes() as $a => $b) {
echo "$a = $b \n";
}
how can add special characters to keys of array
You can use this code:
foreach ($tmp as $key=> $value) {
unset($tmp[$key]);
$tmp["[$key]"] = $value;
}
PHP Array Key encoding?
I tried in Japanese (as is what I can test):
$test["要"]["name"] = "要";
print_r($test);
And the result went fine, as expected.
I'm using UTF-8 for everything. I'm not sure if its a problem with your encoding settings (in php.ini) or the encoding you are using. if that is a problem, why don't you try to encode it with base64? (or other Ascii encoder). That way would be something like:
$test["6KaB"]["name"] = "要";
I'm not sure what is your goal, so let me know if it was useful.
using chinese characters as php associative array keys
To show case a very simple case of using Chinese character in associate key :-
php > $a = array("一定可以"=>TRUE);
php > var_dump($a["一定可以"]);
bool(true)
php > print_r($a);
Array
(
[一定可以] => 1
)
Here is the example if you apply utf8_encode
and utf8_decode
:
php > $j = array(); $j[utf8_encode('大')] = 1; $f = array_keys($j); echo utf8_decode($f[0]);
大
php > print_r($f);
Array
(
[0] => 大 /* this is garbled */
)
php > print_r($j);
Array
(
[大] => 1 /* this is garbled too */
)
One possible way to overcome:
php > print_r(utf8_decode(var_export($j, TRUE)));
array (
'大' => 1,
)
Prepend character to key in PHP array
If you are working in PHP, then I would suggest to do any manipulation in PHP before json_encode
ing.
Say your array is called $arr
:
//Make array of new keys
$newKeys = array_map(function($k){return 'A'.$k;}, array_keys($arr));
//Combine new keys with value
$newArray = array_combine($newKeys, $arr);
How to remove/replace certain characters from both keys and values in an array?
No, there is no way in PHP you can modify an array key by reference. You can only add and remove keys.
Even array_change_key_case
will create a new array instead of modifying the original.
You can create a new array in your function and return the new array. You can then overwrite your original array variable with the clean version.
<?php
function deleteInvalid($dirty, $ikc = [], $ivc = []) {
$clean = [];
foreach ($dirty as $key => $val) {
$cKey = str_replace($ikc, '', $key);
$cVal = str_replace($ivc, '', $val);
$clean[$cKey] = $cVal;
}
return $clean;
}
$dirty = deleteInvalid($dirty);
var_dump($dirty);
You don't need to test the changes of the values after str_replace as it'll be the same if it was not changed. Unless of course your array contains more than strings... in which case you should test the type of value before modifying.
<?php
if (is_string($val)) {
// do something with the string.
}
Related Topics
PHP Glob - Scan in Subfolders For a File
How to Get JavaScript Function Data into a PHP Variable
Best Way to Completely Destroy a Session - Even If the Browser Is Not Closed
Preserve Key Order (Stable Sort) When Sorting With PHP'S Uasort
How to Enable Curl in Wamp Server
Pdo With "Where... In" Queries
How to Change Envelope from Address Using PHP Mail
Initializing PHP Class Property Declarations With Simple Expressions Yields Syntax Error
Adding Minutes to Date Time in PHP
Difference Between Method Calls $Model-≫Relation(); and $Model-≫Relation;
Read Each Line of Txt File to New Array Element
Convert Timestamp to Readable Date/Time PHP
How to Fix the Permission Error When I Call Session_Start()