www.webdeveloper.com
Results 1 to 7 of 7

Thread: function returned array element in single statement

  1. #1
    Join Date
    Mar 2009
    Posts
    452

    function returned array element in single statement

    hi all,

    i am wondering if there is any way to access array elements in a single statement, without defining new function or using loop etc...

    using arrary syntax: getallheaders()['User-Agent']; or object syntax: getallheaders()->User-Agent; or similar. or even a uilt-in function like get_element_by_key(getallheaders(),'User-Agent');


    i've been searching for some time but did not find any.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    I'm only aware of object/method chaining along those lines. If you know where in the array an item will be, you could use array_slice() on the returned value, but that sounds prone to bugs if the array structure ever changes for any reason, and thus not worth the tiny bit of typing it might save (if any?).
    "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

  3. #3
    Join Date
    Oct 2012
    Posts
    4
    a couple of ways you can do it.

    If its a non-associative array you can use the list operator like:

    list($a, $b, $c) = array('a', 'b', 'c');

    With associative arrays, your best using the OO concepts in PHP and using getters and setters. Do some research on magic methods __get and __set.

    Thanks

    Brendan

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Heh...I decided to try something I didn't think would work, but it did:
    PHP Code:
    <?php

    function foo()
    {
        return array(
            
    'foo' => 'FOO',
            
    'bar' => 'BAR'
        
    );
    }

    function 
    arr2obj(array $arr)
    {
        return (object) 
    $arr;
    }

    echo 
    arr2obj(foo())->bar;
    "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 2009
    Posts
    452
    Thanx for reply, i know there are some easy ways to do it by defining custom functions or storing it in a variable (i am using it by storing in a variable then access as i did not find more easy way), I hope some other guys too want same ? isnt it ?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Actually, all you have to do, apparently, is upgrade to PHP 5.4.
    As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

    Example #7 Array dereferencing
    PHP Code:
    <?php
    function getArray() {
        return array(
    123);
    }

    // on PHP 5.4
    $secondElement getArray()[1];

    // previously
    $tmp getArray();
    $secondElement $tmp[1];

    // or
    list(, $secondElement) = getArray();
    ?>
    http://docs.php.net/manual/en/language.types.array.php
    "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

  7. #7
    Join Date
    Mar 2009
    Posts
    452
    That's intereshting.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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