#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 1999
    Location
    Denmark
    Posts
    83
    Rep Power
    15
    Hey there

    I have this problem. I want a function that is able to return a array. But the php-manual says that you can’t..
    Do any of you know a workaround or have any other suggestions?

    Ex. of what I want:

    function arrFunc()
    {
    $returarray = array(“some”,”arrray”,”stuff”);
    return($returarray);
    }


    ------------------
    regds..
    -ulrik-

  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Where does it say that in the manual?

    You CAN return arrays from functions.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 1999
    Location
    Denmark
    Posts
    83
    Rep Power
    15
    Great

    But how do you do it?

    I have done this, and it do not work:
    -------------------------------------
    function arrFunc()
    {
    $returarray = array(“some”,”arrray”,”stuff”);
    return($returarray);
    }
    .
    .
    arrFunc();
    echo $returnarray[2];
    ------------------------------------

    and in the manual it says:

    "You can't return multiple values from a function"

    in the "Returning values" section.




    ------------------
    regds..
    -ulrik-

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    <?

    function arrFunc()
    {
    $list=array("one", "two", "three");
    return $list;
    }

    $newlist=arrFunc();

    for($i=0; $i<count($newlist); $i++)
    print"$newlist[$i]<br>";

    ?>

    The key is assigning the return value from the function to a new value ($newlist) and utilizing the indices in the new variable to do what you want.
  8. #5
  9. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Kyuzo is correct. I'm not sure of the exact verbage in the manual. You cannot return more than one variable, true enough. But that variable can be an array, an object or any other type of variable. It also states that in the manual.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 1999
    Location
    Denmark
    Posts
    83
    Rep Power
    15
    Thanks guys...

    I must re-read the manual I guess.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0
    i know im posting in an incredibly old thread, but i came across this using Google and just wanted to clear something up for anyone else who was wondering.

    You _CAN_ return values, arrays or objects in functions ("Any type may be returned, including arrays and objects.") . The reason his original post didn't work was due to variable scope and the misuse of the return function. Return is used to pass an argument as the function-call, not to set any variables outside of the function itself as his first post was trying to do.

    Also, variables defined inside a function only exist inside said function. the exception being if globals are actively declared inside the running function.

    a proper example would be:
    ---------------
    function arrFunc() {
    $returnarray = array(“some”,”arrray”,”stuff”);
    return $returnarray;
    }
    $mynewarray = arrFunc();
    print_r ($mynewarray);
    ---------------
    :: or ::
    ---------------
    function arrFunc() {
    global $returnarray;
    $mynewarray = array("some,"array","stuff");
    }
    arrFunc();
    print_r ($mynewarray);
    ---------------

    see the following documents on php.net:
    php.net/manual/en/functions.returning-values.php

    php.net/manual/en/language.variables.scope.php

    php.net/manual/en/reserved.variables.globals.php

  14. #8
  15. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,945
    Rep Power
    898
    Oh and also another reason it didn't work the way he expected is because when he first posted this thread, humans were still figuring out how to harness electricity.


    11 year bump for a first post. Legendary.
    Verify and sanitize ALL USER DATA.

    And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.

    Avatar: Stolen by me, shown to me by patrick.

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0
    Originally Posted by angrypanda
    Oh and also another reason it didn't work the way he expected is because when he first posted this thread, humans were still figuring out how to harness electricity.


    11 year bump for a first post. Legendary.
    lol, i know i know its absurdly old, but as stated it was the top result aside of php.net when googling "php function return array", and it had the necessary information for me to learn that you can return arrays in functions (still new to php). & since the core PHP development hasnt changed enough to make any of the provided information false, i signed up purely to clear up why his function wasnt correct in the first place for those that were also new. After all, good code comes from learning why your mistakes failed, not simply getting them to eventually work.
  18. #10
  19. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,995
    Rep Power
    9397
    It's not a true "bump" if it contributes information to the subject. Especially if that information corrects earlier errors or misconceptions.

    lasavior found this thread somehow so there's no doubt that somebody else will too. It's rare that someone will dredge up an old thread for a meaningful contribution; this is one of those cases.

    (With that said, Kyuzo effectively ended the thread when he posted the correct code. There was no explanation of OP's flawed code, yes, but that didn't seem necessary. Regardless, I'm leaving the new stuff intact.)

    Comments on this post

    • angrypanda agrees : ur a true bump
    Last edited by requinix; April 27th, 2011 at 03:14 AM.

Similar Threads

  1. Uploading Files
    By vivekjain in forum ASP Programming
    Replies: 0
    Last Post: February 16th, 2004, 11:21 PM
  2. 2d array passed to a function
    By capaci in forum C Programming
    Replies: 1
    Last Post: February 13th, 2004, 09:54 PM
  3. help with an isGreater function
    By qiushiming in forum C Programming
    Replies: 18
    Last Post: February 5th, 2004, 08:23 AM
  4. return pointer to array question
    By theRaginCajun in forum C Programming
    Replies: 5
    Last Post: January 29th, 2004, 05:06 PM
  5. return contents of directory in array
    By jacen6678 in forum C Programming
    Replies: 1
    Last Post: January 26th, 2004, 03:32 PM

IMN logo majestic logo threadwatch logo seochat tools logo