douzhuo2722 2018-05-08 08:24
浏览 118
已采纳

尽管已设置,但Isset返回未定义的索引

I have a weird problem. I call a self written function which returns my SQL result.

$isBusinessAddress = PartnernetShop::invoiceAddressIsBusinessAddress($userID)->toArray();

I want to get the meta_value of the first array cause this is the returned value I need. For security reasons I check if the index and value exits:

if(isset($isBusinessAddress[0]["meta_value"])){ ... }

This if statement returns:

Undefined index: meta_value

However, if I check the value just 2-3 lines above it works!

var_dump($isBusinessAddress);
var_dump(isset($isBusinessAddress[0]["meta_value"]));
var_dump(array_key_exists("meta_value", $isBusinessAddress[0]));
var_dump($isBusinessAddress[0]["meta_value"]);

Output:

array(1) {
  [0]=>
  array(2) {
    ["user_id"]=>
    int(12)
    ["meta_value"]=>
    string(4) "Nein"
  }
}
bool(true)
bool(true)
string(4) "Nein"

This is my code:

$isBusinessAddress = PartnernetShop::invoiceAddressIsBusinessAddress($userID)->toArray();

var_dump($isBusinessAddress);
var_dump(isset($isBusinessAddress[0]["meta_value"]));
var_dump(array_key_exists("meta_value", $isBusinessAddress[0]));
var_dump($isBusinessAddress[0]["meta_value"]);
//die();

if(isset($isBusinessAddress[0]["meta_value"])){
    $isBusinessAddress = [0]["meta_value"];
}else{
    $isBusinessAddress = "NotSet";
}

Can you guys tell me what I am doing wrong? I have used this if statement already multiple times...

UPDATE: The index definitely exists cause I pass the if statement. I just get message "Undefined index: meta_value" on the line trying to assign the value to the variable! Following line:

$isBusinessAddress = [0]["meta_value"];

Kind regards and Thank You!

  • 写回答

1条回答 默认 最新

  • duanmangxie7131 2018-05-08 08:28
    关注

    The problem is not the isset(), but the line afterwards:

    $isBusinessAddress = [0]["meta_value"];
    

    which tries to get the key meta_value from the array [0] (resp. array(0 => 0)) where this key does not exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?