Radasca
Radasca

Reputation: 1

Access array key returned by a function

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

Upvotes: 0

Views: 194

Answers (1)

GautamD31
GautamD31

Reputation: 28763

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'];

Upvotes: 2

Related Questions