douqianxian7008 2016-11-08 19:10
浏览 20

我似乎有一个无法访问的数组元素[关闭]

I try to access an array element, but for some reason this gives me some trouble.

So I got this array inside $lead:

Array
(
    ["City"] => Other
    [Company Name] => 
    [Company Email] => 
    [Contact Form URL] => 
    [Comments] => 
)

And I try to access it like this:

var_dump($lead['"City"']);

But for some reason I get an undefined index error:

Notice: Undefined index: "City"

And I have no idea why that is?

EDIT:

This is the output when I use highlight_string(print_r($lead, TRUE));:

Array
(
    ["City"] => Other
    [Company Name] => 
    [Company Email] => 
    [Contact Form URL] => 
    [Comments] => 
)

Also with this code:

foreach($lead as $k => $v){
    var_dump($k);
    die();
}

I get:

string(9) ""City""
  • 写回答

1条回答 默认 最新

  • dongtangu6144 2016-11-08 19:26
    关注

    So thanks to the comments it seems like I got an UTF-8 BOM character (EF BB BF) at the start of my key and that is why I couldn't access it. It also seemed that Stack Overflow stripped away the character when I posted it here.

    With this code:

    foreach($lead as $k => $v){
        var_dump($k);
        die();
    }
    

    It showed that the key had to be 9 bytes long.

    string(9) ""City""
         //↑
    

    With the help of a hex viewer you could also see the EF BB BF character. So the solution is to use a foreach loop to get the array element or use the correct key:

    foreach($lead as $k => $v){
        echo $v;
        break;
    }
    

    or

    echo $lead[chr(0xEF) .  chr(0xBB) . chr(0xBF) . '"City"'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大