Home > Software design >  how to we access objects with array?
how to we access objects with array?

Time:09-23

I have a dump of data :

var_dump($steps); 

and results are :

object(Drupal\form\Manager\StepManager)#490 (1) {
      ["steps":protected]=>
      array(1) {
        [1]=>
        object(Drupal\form\Step\StepOne)#494 (2) {
          ["step":protected]=>
          int(4)
          ["values":protected]=>
          array(1) {
            ["key"]=>
            string(3) "123"
          }
        }

but I tried using :

$steps[1]->values->key but its having an error, its not available directly?

where did I miss?

CodePudding user response:

The index to the first item – and only item in your case – is 0, and not 1. So this should work:

$steps[0]->...

The number 1 in the var_dump shows the size of the array:

["steps":protected]=>
      array(1) {  // This array contains one item

CodePudding user response:

Your $steps array with in a object of Drupal\form\Manager\StepManager and you accessed key from values object but your output shows you have a values array not object.

array(1) {
            ["key"]=>
            string(3) "123"
          }

So try this in var_dump() I think its worked

var_dump($steps->steps[1]->values["key"]);
  • Related