1

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.

13
  • 1
    Try var_dump, which gives you more info, like the string length. Make sure there are no invisible characters in there. Commented Oct 20, 2011 at 4:31
  • 2
    This doesn't solve your immediate problem but I'd recommend moving to an object-oriented approach with clearly defined properties / methods. Commented Oct 20, 2011 at 4:33
  • 1
    I think you should have used this: if($value[0]['property']['typeKind'] == 'isolation'){ // some action here } Commented Oct 20, 2011 at 4:34
  • @WesleyMurch He did array_keys($value). Commented Oct 20, 2011 at 4:35
  • 1
    Are you sure every element in $data has the key property ? Commented Oct 20, 2011 at 4:43

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your tip, but it says that index '0' is not found. By the way, the array that's in the if caluse is not numerically indexed.
I totally rewrote my answer. ;)
Thanks Stephen! One of the arrays in the nest did not contain the index property. Since the array was too huge and the actual function was recursive, it went unnoticed. Thank you very much for the insight!
2

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

1 Comment

Yes Farray! My array didn't have the index at one point. Fixed that, and everything works normal! Thank you for the guidance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.