Getting element from PHP array returned by function

后端 未结 7 1081
野趣味
野趣味 2020-12-11 01:10

I\'m not sure if this is possible, but I can\'t figure out how to do it if it is...

I want to get a specific element out of an array that is returned by a function,

相关标签:
7条回答
  • 2020-12-11 01:58

    PHP cannot do this yet, it's a well-known limitation. You have every right to be wondering "are you kidding me?". Sorry.

    If you don't mind generating an E_STRICT warning you can always pull out the first and last elements of an array with reset and end respectively -- this means that you can also pull out any element since array_slice can arrange for the one you want to remain first or last, but that's perhaps taking things too far.

    But despair not: the (at this time upcoming) PHP 5.4 release will include exactly this feature (under the name array dereferencing).

    Update: I just thought of another technique which would work. There's probably good reason that I 've never used this or seen it used, but technically it does the job.

    // To pull out Nth item of array:
    list($item) = array_slice(getSomeArray(), N - 1, 1);
    
    0 讨论(0)
提交回复
热议问题