0

Good day, Im very confused why I keep on getting an undefined error whenever I tried to use '$x->split' when iterating through my array.

dd

$divs = array(3) {
  ["items"]=>
  array(4) {
    [0]=>
    object(stdClass)#247 (13) {
      ["type"]=>
      int(6)
      ["description"]=>
      NULL
      ["transactionType"]=>
      string(4) "Bill"
      ["account_xid"]=>
      int(4)
      ["gt_xid"]=>
      int(1)
      ["u_xid"]=>
      int(2001)
      ["po_xid"]=>
      int(0)
      ["debit"]=>
      float(144)
      ["credit"]=>
      float(0)
      ["date_at"]=>
      string(19) "2014-10-17 19:37:50"
      ["ref_no"]=>
      string(2) "22"
      ["id"]=>
      int(1)
      ["split"]=>
      array(1) {
        [0]=>
        object(stdClass)#246 (1) {
          ["gt_xid"]=>
          int(1)
        }
      }
    }
    [1]=>
    object(stdClass)#248 (12) {
      ["type"]=>
      int(6)
      ["description"]=>
      NULL
      ["transactionType"]=>
      string(4) "Bill"
      ["account_xid"]=>
      int(4)
      ["gt_xid"]=>
      int(2)
      ["u_xid"]=>
      int(2001)
      ["po_xid"]=>
      int(0)
      ["debit"]=>
      float(0)
      ["credit"]=>
      float(0)
      ["date_at"]=>
      string(19) "2014-11-15 01:40:00"
      ["ref_no"]=>
      string(0) ""
      ["id"]=>
      int(4)
    }
    [2]=>
    object(stdClass)#249 (13) {
      ["type"]=>
      int(6)
      ["description"]=>
      NULL
      ["transactionType"]=>
      string(4) "Bill"
      ["account_xid"]=>
      int(4)
      ["gt_xid"]=>
      int(3)
      ["u_xid"]=>
      int(2001)
      ["po_xid"]=>
      int(0)
      ["debit"]=>
      float(2980)
      ["credit"]=>
      float(0)
      ["date_at"]=>
      string(19) "2014-11-15 06:25:55"
      ["ref_no"]=>
      string(4) "0998"
      ["id"]=>
      int(6)
      ["split"]=>
      array(1) {
        [0]=>
        object(stdClass)#254 (1) {
          ["gt_xid"]=>
          int(3)
        }
      }
    }
    [3]=>
    object(stdClass)#250 (13) {
      ["type"]=>
      int(6)
      ["description"]=>
      NULL
      ["transactionType"]=>
      string(4) "Bill"
      ["account_xid"]=>
      int(4)
      ["gt_xid"]=>
      int(4)
      ["u_xid"]=>
      int(2001)
      ["po_xid"]=>
      int(0)
      ["debit"]=>
      float(500)
      ["credit"]=>
      float(0)
      ["date_at"]=>
      string(19) "2014-11-15 06:37:07"
      ["ref_no"]=>
      NULL
      ["id"]=>
      int(8)
      ["split"]=>
      array(1) {
        [0]=>
        object(stdClass)#256 (1) {
          ["gt_xid"]=>
          int(4)
        }
      }
    }
  }
  ["accountName"]=>
  string(15) "Inventory Asset"
  ["startingBalance"]=>
  int(0)
}

the thing is Im using that kind of format when I iterate through my array on my Items. here's the way I do it.

@foreach($divs as $div)
 {{ $div['accountNam'] }}
 @foreach($div['items'] as $x)
 {{ $x->description }}
 {{ $x->date_at }}
 {{ $x->transactionType }}
 @endforeach
@endforeach

that code above works fine but when I add the 3rd inner loop

 @foreach($divs as $div)
  {{ $div['accountNam'] }}
  @foreach($div['items'] as $x)
    {{ $x->description }}
    {{ $x->date_at }}
    {{ $x->transactionType }}
       @foreach($x->split as $z)
        ----I get error here---
       @endforeach
  @endforeach
@endforeach

I get this error: Undefined property: stdClass::$split

when I tried to dd($z->split)

I get this result which means that its not empty or doesnt exist

array(1) { [0]=> object(stdClass)#246 (1) { ["gt_xid"]=> int(1) } } 

Can You Please help me find the thing that I am missing here. To make it work. I already tried using $z['gt_xid'] but it gives me another error : Cannot use object of type stdClass as array .. Thank you for spending time to read this. have a nice day ahead!

2
  • Why are you prepending each and every control structure with an @? This specifically tells PHP to ignore any possible errors and warnings generated there, thus making it a LOT harder for you to debug the code. Get rid of them and you'll have a much easier time debugging. Commented Dec 10, 2014 at 14:22
  • im using blade templating here :) Commented Dec 10, 2014 at 14:55

2 Answers 2

1

in your array $div['items'] is one element (index 1) without the split property.

if this may happen, you need to guard against it. I don't know the syntax you are using, but it will follow along these lines:

if (isset($x->split) :
  @foreach($x->split as $z)
    // do stuff
  @endforeach
endif;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that @cypherabe.. I did not realized it right away.. :)
1

I believe the 2nd element (key 1) of your array doesn't have a "split" value. You should test if "split" is defined (isset($x->split)) before trying to loop in it.

Comments

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.