在数组中找不到索引


PHP - Index not found in array

我有一个数组$attribGarden,它看起来像这样:

array(3) {
  [0]=>
  array(2) {
    ["property"]=>
    array(3) {
      ["typeKind"]=>
      string(9) "isolation"
      ["typeId"]=>
      int(76)
      ["valueId"]=>
      string(3) "386"
    }
    ["children"]=>
    array(2) {
      [0]=>
      array(2) {
        ["property"]=>
        array(3) {
          ["typeKind"]=>
          string(9) "isolation"
          ["typeId"]=>
          int(79)
          ["valueId"]=>
          string(3) "395"
        }
....
....
....

我也有一个函数在同一页:

function ____storeAttribGarden($data, $parent = 0){
    foreach($data as $value){
        if($value['property']['typeKind'] == 'isolation'){
            // some action here
        }
    }
}

当代码执行时,它抛出这个错误:

未定义的索引:属性在E:'xyz'proc_product.php在第1743行

//第1743行引用函数

的if()条件

我在if条件之前尝试了print_r(array_keys($value));,并得到了以下输出:

Array
(
    [0] => property
    [1] => children
)

print_r($value)给出如下:

Array
(
    [property] => Array
        (
            [typeKind] => isolation
            [typeId] => 76
            [valueId] => 386
        )
    [children] => Array
        (
            [0] => Array
                (
                    [property] => Array
                        (
                            [typeKind] => isolation
                            [typeId] => 79
                            [valueId] => 395
                        )

所以很明显,在数组中有一个名为'property'的索引。但是函数不能识别它。有什么问题吗?我做错什么了吗?

感谢您的宝贵时间。

一开始我以为你只是把层次结构弄乱了。但是,我假设您像这样传递$attribGarden:

____storeAttribGarden($attribGarden);

如果是,检查是否连续出现三个错误,还是只有一个错误。如果你只得到一个,那么$attribGarden的结构可能是不均匀的。

要么是这样,要么是我最初的假设是正确的,你的层次结构仍然是错误的。

我认为Stephen是对的,数据结构不统一。一种简单的调试方法是将以下行放在您的if语句之前:

if ( !isset( $value[ 'property' ][ 'typeKind' ) ) print_r( $value );