PHP:特殊字符作为数组中的键

人气:884 发布:2022-10-16 标签: php special-characters arrays key

问题描述

我的问题是我使用特殊字符&;作为密钥,这似乎不起作用

我的数组是这样的

$legalforms = array(
    'GmbH & Co.KG' => array(
            'namesToSubmit' =>array(
                'companyName'=>'required',
                'location'=>'required', 
                'phone'=>null,
                'fax'=>null,
                'web'=>null,
                'registryCourt'=>'required',
                'registryNumber'=>'required',
                'companyNameAssociate'=>'required',
                'locationAssociate'=>'required',
                'registryCourtAssociate'=>'required',
                'registryNumberAssociate'=>'requuired',
                'ceo'=>'required'
            ),
       )
)

当我想要使用名称ToSubmit时,我得到一个错误,即nameToSubmit的属性为空,如果我删除其中的特殊字符&;,它就可以工作。那么,如何使其与&;一起使用?

编辑:

$("#sendLegalForm").click(function () { 
         selection = $('#selection').val();
         $.ajax({
             type:'GET',
             url:'http://192.168.10.24/php/sig.php?selectedLegalform='+ selection,
             dataType:'json',
             success: function (data){  

                 $("#legalform").hide();
                 $("#fields").show();
                 var fieldnames =[];
                 for(property in data.namesToSubmit){
                    fieldnames.push(property);
                 }
                 var fields=[];
                 for(var i=0; i<data.textfieldHeaders.length; i++){               
                 fields.push(data.textfieldHeaders[i],'<br>','<input name="',fieldnames[i],'" type="text"                                                       ',data.namesToSubmit[fieldnames[i]] == "required"?"required":"",'>','<br/>');        
                 }                

                 fields.push("<br>", 'Pflichtfelder (*)');
                 $("#fieldsForm").prepend(fields.join(''));              
             },
             error: function(jqXHR,textStatus,errorThrown){
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
             }
         });
     });

我在该行中看到错误

for(property in data.namesToSubmit){

尝试了MD5,不起作用,但谢谢您的帮助

推荐答案

将请求方法改为POST

$.ajax({
    type:'POST',
    url: 'http://192.168.10.24/php/sig.php',
    data: {selectedLegalform: selection},
    ...

在PHP中:

$data = $_POST['selectedLegalform'];
如果通过GET发送字符串GmbH & Co.KG,它将变为GmbH%20%26%20Co%2EKG。这应该是问题所在。

865