Nirmal
Nirmal

Reputation: 9549

PHP - Index not found in array

I have an array $attribGarden which looks like this:

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"
        }
....
....
....

Also I have a function on the same page:

function ____storeAttribGarden($data, $parent = 0){

    foreach($data as $value){

        if($value['property']['typeKind'] == 'isolation'){
            // some action here
        }
    }
}

When the code executes, it's throwing this error:

Undefined index: property in E:\xyz\proc_product.php on line 1743

// line 1743 refers to the if() condition of the function

I tried print_r(array_keys($value)); just before the if condition, and got the following output:

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

The print_r($value) gives this:

Array
(
    [property] => Array
        (
            [typeKind] => isolation
            [typeId] => 76
            [valueId] => 386
        )

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

So it's clear that there is an index called 'property' in the array. But the function does not recognise it. What could be the problem? Am I doing anything wrong here?

Thanks for your time.

Upvotes: 1

Views: 2666

Answers (2)

Farray
Farray

Reputation: 8528

I'm thinking Stephen is right and the data structure is not uniform. A simple way to debug would be to put the following line before your offending if statement:

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

This will help you find the spot in your data structure where things are going haywire...

Upvotes: 2

Stephen
Stephen

Reputation: 18964

At first I thought that you had simply messed up your hierarchy. However, I assume you are passing $attribGarden like this :

____storeAttribGarden($attribGarden);

If so, then check if you are getting three errors in a row, or just one. If you are getting just one, then $attribGarden's structure is probably not uniform.

Either that, or my original assumption was correct, and your hierarchy is still off.

Upvotes: 4

Related Questions