0

i have a function that returns an array with keys and values, like this

function someinfo($x, $y){

    //something done by the function
    //the array will be like

    $info=array(
        "name" => "aaa",
        "email" => "[email protected]"
    );

    return  $info;
}

i can do this

$returned_info=someinfo(1,2);
print $returned_info['name'];

but i want something in one line of code, like:

print $someinfo(1,2)['name'];

how can i print the values from the returned array in one single line?

thanks, have a nice day

3

1 Answer 1

2

Try to pass the return key like

function someinfo($x, $y , $key){

    $info=array(
        "name" => "aaa",
        "email" => "[email protected]"
    );
    return  $info[$key];
}

and print like

print($someinfo(1,2,'name'));

And PHP 5.4.0 facilitates the this Short array syntax even

print $someinfo(1,2)['name'];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, it is a good idea. i made like this - function ($x, $y, $key="") and made an if inside the function that verifies if the $key has a value or not and returns the entire array or only the key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.