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 7 of 7
  1. #1
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts

    Returning values from functions syntax problem

    Here's a simple explanation of what i don't understand. Probably beacouse i didn't read good enough when i learned php.

    Code is basicly a function that returns array, which parts i need to use in another function. Like this

    PHP Code:
    $array = array ("link1" => "value1""link2" => "value2");

    function 
    getKeys ($value) {
    return 
    array_keys($value); #this is supposed to get an array like "link1", "link2"
    }

    //function which calls getKeys and is supposed to use that array

    function useKeys($array) {
    echo 
    getKeys($array)[0];

    I guess that's a syntax problem, but i just can't figure it out.
    Thanks in advance!
    (p.s. i don't have any rep to give you, but i would appreciate your help)

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    $array = array ("link1" => "value1""link2" => "value2");

    function 
    useKeys($array) {
    $arraykeys array_keys($array);
    echo 
    $arraykeys[0];

    You dont really need that first function.

  • #3
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Mwnciau View Post
    PHP Code:
    $array = array ("link1" => "value1""link2" => "value2");

    function 
    useKeys($array) {
    $arraykeys array_keys($array);
    echo 
    $arraykeys[0];

    You dont really need that first function.
    i need to figure that from post #1. i need it in another contex, this is just simple example..

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    echo getKeys($array)[0]; 
    Yeah you can't reference an array in this fashion; I know it looks like you should be able to do it since you can do echo $anArray[0]; and the result of calling getKeys() is an array, but that is just the point-- the result of getKeys() is an entire array object; you can't just append an index to an array object.

    I'm not explaining it very well, I know... just think of $anArray[0] as its own independant variable that is encompassed by the array object $anArray.

    I'm still not explaining it very well But this is a simple problem to circumvent. Simply assign the result of the function to a variable and then access the elements inside the array via the new variable.
    PHP Code:
    $returnArray =  getKeys($array);
    echo 
    $returnArray[0]; 

  • #5
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    I think i took a much larger bite than i can swallow. I hope you understand that fraze. It's just that everything is so messy inside my head, i can't pull single straight thought, without loosing the big picture. I think that i maxed out my brain capacity and it's going to shutdown really soon.

    And all what i'm trying to do is create and unordered list from X arrays. And there is so many problems, and that foreach just sux. It's not good for nothing. Sorry for venting, but just the thought of learning oop makes me sick.

    sending kill signal..

    why isn't just a thought, or a wish to do something good enough to do the same. bleeep..

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    OK, after a hard days night.. Few minutes after the vent post i got an idea. It's really strange but i usually get new ideas right after posting my problem on forums. And ppl say that we are not only one j/k

    I figured out a way to solve my situation, and that is calling a function froma an array. Sounds crazy, but it should work, and with your help i might even manage it. Here's the deal

    PHP Code:
    <?php

    $croatianlang 
    "čćđ";
    $variable callSub($croatianlang);

    function 
    callSub ($croatianlang){

    echo 
    "Yes";

    }
    $array2 = array ("sslink1" => $variable"sslink2" => "ssvalue2");


    function 
    createMenu ($array2) {

        foreach (
    $array2 as $key => $value) {
        
        echo 
    "<ul>\n";
        echo 
    "\t<li>".$key."\n";
        
        echo 
    "\t\t<ul>\n";
        echo 
    "\t\t\t<li>".$value."</li>\n";
        
        echo 
    "\t\t</ul>\n";
        echo 
    "\t</li>\n";
        echo 
    "</ul>\n";
        
        }


    }

    ?>
    But the problem is that that "Yes" echos outside the foreach loop, and i need a way to set that $variable, before looping.

    What am i doing wrong?

  • #7
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Solved

    Return instead of echo


  •  

    Posting Permissions

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