The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 12 of 12

Thread: PHP function's returned array syntax?

  1. #1

    PHP function's returned array syntax?

    Let's just say that function foo returns an array, and i want to access an item in the array. In actionscript i could do something like this:
    myVar=foo()["item"];


    currently in php i can only accomplish this with 2 lines of code:
    myVarTemp=foo();
    myVar=myVarTemp["item"];


    is there a faster way in php?

  2. #2
    What situation are you in that is keeping you from just using the array?

    Run:
    Code:
    $myVar = foo();
    And reference with:
    Code:
    echo $myVar['item1'];
    echo $myVar['item2'];
    echo $myVar['item3'];
    P.S. Don't forget the $ symbol when using Vars in PHP. Common error coming from AS or JS.
    "Give me the place to stand, and I shall move the earth" - Archimedes
    "The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." - Bertrand Russell

  3. #3
    oops about the $

    It's not about not being able to access the array, it's about how to access an item in a returned array without having to declare a temporary variable first.


    Quote Originally Posted by SlicDiggy View Post
    What situation are you in that is keeping you from just using the array?

    Run:
    Code:
    $myVar = foo();
    And reference with:
    Code:
    echo $myVar['item1'];
    echo $myVar['item2'];
    echo $myVar['item3'];
    P.S. Don't forget the $ symbol when using Vars in PHP. Common error coming from AS or JS.
    Last edited by recoilfx; October 22nd, 2008 at 09:52 AM.

  4. #4
    Quote Originally Posted by recoilfx View Post
    It's not about not being able to access the array, it's about how to access an item in a returned array without having to declare a temporary variable first.
    The following as suggested would work, did you try it?
    Quote Originally Posted by SlicDiggy View Post
    What situation are you in that is keeping you from just using the array?

    Run:
    Code:
    $myVar = foo();
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  5. #5
    You don't need to assign a temporary variable first. You can access the contents of the array without taking them out of it, as outlined in my previous post.
    "Give me the place to stand, and I shall move the earth" - Archimedes
    "The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." - Bertrand Russell

  6. #6
    I'm going to point out that if you are calling a function that returns an array just so you can access one element, then the ID should be taken as a parameter and return the element. Is there a reason why the function doesn't do this?

    I understand completely though that accessing a returned array directly like you posted should be possible (it works in most other languages).

  7. #7
    You don't need to assign a temporary variable first. You can access the contents of the array without taking them out of it, as outlined in my previous post.
    Sorry, i guess I wasn't being clear. I know i can access the item by using something like $myVars['item'], but if I need to use that element "item" many times, i would end up typing $myVars['item'] again and again, i was just wondering if i could assign the element to an variable in one go, just like in actionscript.

    Oh well, every language has its quirks....

  8. #8
    14
    posts
    Registered User
    Quote Originally Posted by recoilfx View Post
    Sorry, i guess I wasn't being clear. I know i can access the item by using something like $myVars['item'], but if I need to use that element "item" many times, i would end up typing $myVars['item'] again and again, i was just wondering if i could assign the element to an variable in one go, just like in actionscript.

    Oh well, every language has its quirks....
    In my opinion its not a problem to use $myVars['item'] all the time and neither is makeing a $newVar = $myVars['item'] variable. But if you really dont want that you can write your function different.

    Write your function that way that you have to give the required array key as a function parameter.

    So that you do
    PHP Code:
    $myVar foo("item"); 
    and that the function checks which array key it has to return. That way your $myVar will be that array key which you were looking for!

  9. #9

    Recycle Bin

    PHP Code:
    function getReturnedArrayValue(&$array$key) {
        return 
    $array[$key];
    }

    echo 
    getReturnedArrayValue($obj->getArr(), 'theKey'); 
    You could use function like this, of course it will only work with arrays with 1 level of depth.



    Last edited by martin_georgiev; October 4th, 2008 at 04:40 PM.

  10. #10
    no, as far as i know. if you are able to redefine the function, do it like this

    Code:
    function foo($key=null)
    {
    return isset($key) ? $arr[$key] : $arr;
    
    }
    or

    Code:
    $_=foo();
    $myVar=$_["item"];
    
    // instead of 
    
    $myVarTemp=foo();
    $myVar=$myVarTemp["item"];
    because the $_'s useful life minimally short, so doesn't matter the variable name isn't descriptive.

  11. off topic:
    -------------------------

    It's funny... I last looked at this thread over a half a year ago, and I returned because of the thread subscription notification. What the hell was I thinking?

    "Give me the place to stand, and I shall move the earth" - Archimedes
    "The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." - Bertrand Russell

  12. #12
    didn't realize it's that old. why would he bring that old back?

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
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012