Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    17
    Thanks
    5
    Thanked 1 Time in 1 Post

    accessing part of an array from a function return

    Hi, I want to access a aprt of an array that is returned from a function, by my method seems a little wasteful, using an extra variable. I just think there is a PHP way to go direct! Im just trying to be uber efficient with my code!

    ok, so I have a function that returns an array, and I use it to convert a variable to its correct entry
    PHP Code:
    $town "LO"
    $array fulltown$town );

    echo 
    $array['longtown'] ; // London
    echo $array['URL']; // london.co.uk
    echo $array['caps']; // London Town 
    I just want a straight variable, as I use it in a mysql_query - and that doesnt like $var['x'] due to the quote symbols ( unless im doing that wrong too )

    eg

    PHP Code:
        $tempfulltown$town );
        
    $town $temp['longtown']; 

    I want to do something like below but It doesnt work - to avoid the $temp requirement

    PHP Code:

    $town 
    fulltown$town )['longcode'] ; // not working php
    or
    $town = ['longtown']fulltown$town);// not working php 
    whats the way to avoid using a temp variable?

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    You can't avoid the $temp variable. PHP doesn't support the method of coding you're trying to do.

    I'm afraid the only way to do it is the very way you're trying to avoid.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    You obviously can't use it the way you are trying to do. temp variable is also needed, you can use it as $array['longtown'] anywhere you want - whether mysql query or something else. As long as you concatename the string properly, there is absolutely no issue and temp variable is a waste.

    There is nothing like a straight variable, you are being silly in attempt to remove ' from variable. That is not how you should approach it.
    Hosting Reviews and Discounts: Bluehost Coupon and Hostmonster Coupon

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    What does you're fulltown() function look like? If it accepts no other parameters, you could add a second optional parameter that reflects the key name you're looking for:

    If you have:
    PHP Code:
    function fulltown$town )
    {

        
    /* ... stuff here ... */

        
    return $array;

    ...then change it to:
    PHP Code:
    function fulltown$town$key NULL )
    {

        
    /* ... stuff here ... */

        
    if ( isset( $key$town$key ] ) )
        {
            return 
    $town$key ];
        }
        return 
    $array;

    ...if you're that concerned over multiple lines. But an extra line in there really isn't a big deal.

    The PHP people were recently mulling over a bunch of changes in array handling for upcoming releases, and the syntax you're attempting is one of them... though I haven't read up on it lately.

  • Users who have thanked kbluhm for this post:

    s2ooz (11-17-2011)

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by kbluhm View Post
    The PHP people were recently mulling over a bunch of changes in array handling for upcoming releases, and the syntax you're attempting is one of them... though I haven't read up on it lately.
    yea, function/array dereferencing (i.e. $val = myfunction()[$key]) is scheduled for PHP 5.4.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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