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

时间:2013-07-25 12:58:23

标签: php arrays key special-characters

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

我的数组就像这样

$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'
            ),
       )
)

当我想使用namesToSubmit我得到一个错误,nameToSubmit的属性为null,如果我删除它的特殊字符& ,它的工作..所以我怎么能得到它适用于&

编辑:

$("#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,没有用,但谢谢你的帮助

3 个答案:

答案 0 :(得分:3)

来自PHP Manual

  

密钥可以是整数或字符串。值可以是任何值   类型。

     

此外还会发生以下关键演员:

     
      
  • 包含有效整数的字符串将强制转换为整数类型。例如。键“8”实际上将存储在8下。另一方面,“08”将不会被强制转换,因为它不是有效的十进制整数。
  •   
  • 浮点数也会转换为整数,这意味着小数部分将被截断。例如。密钥8.7实际上将存储在8下。
  •   
  • Bool也会被转换为整数,即键值true实际上将存储在1下,键值为0表示错误。
  •   
  • Null将被强制转换为空字符串,即密钥null实际上将存储在“”。
  • 下   
  • 数组和对象不能用作键。这样做会导致警告:非法偏移类型。
  •   

因此,不是在数组键中使用&,而是可以使用str_replace()将其替换为允许的字符,一旦声明了数组,就可以将其替换为&amp ;.

另一种方法是使用md5(),如建议here

md5('GmbH & Co.KG' => array( ...

希望这有帮助!

答案 1 :(得分:2)

将请求方法更改为POST

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

在PHP中:

$data = $_POST['selectedLegalform'];

如果您通过GmbH & Co.KG发送字符串GET,则会变为GmbH%20%26%20Co%2EKG。这应该是问题。

答案 2 :(得分:1)

您可以替换'&amp;'在数组中使用'-and-'并在使用数组时再次替换。希望它会有所帮助。我通常会这样做。

替换,使用str_replace()