www.webdeveloper.com
Results 1 to 15 of 15

Thread: referencing a function that returns an array

  1. #1
    Join Date
    Mar 2005
    Posts
    43

    referencing a function that returns an array

    Is it possible to directly reference an element in an array returned by a function, without assigning the array to a variable first? Like this:

    PHP Code:
    $the_element_I_want function_that_returns_an_array()[2
    I've never found a way to do it (not tried massively hard though), but it bugs me, because its very easy in, for example, java & javascript.

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,926
    You can get the first or last value via array_shift() or array_pop(), respectively.
    PHP Code:
    $firstValue array_shift(function_that_returns_array());
    $lastValue array_pop(function_that_returns_array()); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,926
    Untested, but seems like it should work to get a value from the middle:
    PHP Code:
    $midValue array_shift(array_slice(function_that_returns_array(), 2)); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2005
    Posts
    43
    Yeah any function to do it is pretty simple. I'm using
    Code:
    function arrayElement($arr, $index)
    {
    	if(isset($arr[$index]))	return $arr[$index];
    	else return false;
    }
    But I'm still annoyed that there's no built-in construct to do it.

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by hessodreamy
    PHP Code:
    function arrayElement($arr$index)
    {
        if(isset(
    $arr[$index]))    return $arr[$index];
        else return 
    false;

    This is bad because the array element itself may itself contain false, leading to misleading results.

  7. #7
    Join Date
    Mar 2005
    Posts
    43
    Yes indeed. It worked for what I was doing at the time, but yeah I'll have to rework it for future use. Cheers

  8. #8
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Why would you want to? function results are not static - for each request they would get rerun, thus slowing down the performance of the system. if you can guarantee that it will only reference one element, then it should be fine to do.

    Also, you could use a wrapper class to do this, rather than what is being proposed.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  9. #9
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by chazzy
    Why would you want to?
    It just means you can get the value of an element part way through an array without setting an intermediate variable. Obviously though if you need more than one value from that array it would unnecesarilly raise the workload.

  10. #10
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Quote Originally Posted by bokeh
    It just means you can get the value of an element part way through an array without setting an intermediate variable.
    I'm not sure what you mean by this statement.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  11. #11
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by chazzy
    I'm not sure what you mean by this statement.
    Well in javascript if I want to access the third link in the second paragraph I can do this:
    PHP Code:
    LINK document.getElementsByTagName('p')[1].getElementsByTagName('a')[2].href 
    In PHP it would be more like:
    PHP Code:
    doc document
    paragraphs 
    doc.getElementsByTagName('p')
    paragraph_2_links paragraphs[1].getElementsByTagName('a')
    link_3 paragraph_2_links[2]
    link_3_url link_3.href 
    In the second example a lot of unnecessary arrays are being set.

  12. #12
    Join Date
    Mar 2005
    Posts
    43
    Untested, but seems like it should work to get a value from the middle:

    PHP Code:
    $midValue = array_shift(array_slice(function_that_returns_array(), 2));
    True that my function could return a FALSE element and give misleading results. However using the above code does not give an error if the required index does not exist, which might be what you want but is different behaviour from normal array referencing. Using this function:
    Code:
    function arrayElement($arr, $index) 
    { 
        return $arr[$index]; 
    }
    accurately emulates the referencing of the array (or seems to) ie it will reference the element if it's there, otherwise kick off an error. And it circumvents the necessity of setting a variable for the array.

    Naturally, if you're going to be referencing more than 1 element in the array you would set it to a variable. You pick that up pretty early in java type stuff.

  13. #13
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    When you do this ...
    PHP Code:
    function arrayElement($arr$index) { 
        return 
    $arr[$index]; 

    ... PHP does create an array, even if only temporarily. From a memory or CPU standpoint, there's no advantage there. I understand the issue of not wanting to use a $variable name for the results of a function. But is it worth the overhead?

  14. #14
    Join Date
    Mar 2005
    Posts
    43
    I understand that an array is created. It's purely cleanness of code that I'm trying to attain here, not performance improvements.

  15. #15
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    That much I understood.

    When I referred to "overhead", I not only meant performance but human overhead, as well.

    When you start using functions over functions to "hide" something, in this case an array, you don't necessarily contribute to that cleanness. As long as you're writing code for personal use, all is fine. But if an error pops up and your code is visited by another programmer, it may become an issue.

    Of course, I realize, it's all in the definition of "cleanness".

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles