Access array returned by a function in php

前端 未结 5 570
抹茶落季
抹茶落季 2020-11-22 12:43

I\'m using a template engine that inserts code in my site where I want it.

I wrote a function to test for something which is quite easy:

myfunction()         


        
5条回答
  •  误落风尘
    2020-11-22 13:00

    You cannot use something like this :

    $this->getData()['a']['b']
    

    ie, array-access syntax is not possible directly on a function-call.

    Youy have to use some temporary variable, like this :

    $tmp = $this->getData();
    $tmp['a']['b']    // use $tmp, now
    

    In your case, this probably means using something like this :

    function myfunction() {
      $tmp = $this->getData();
      return ($tmp['a']['b'] ? true : false);
    }
    

    You have to :

    • first, call your getData() method, and store its return value in a temporary varibale
    • then, use that temporary variable for your test

    You don't have much choice about that, actually...

提交回复
热议问题