I'm trying to have this function return multiple results in array(?).
So basically instead of running lookup("354534", "name");
and lookup("354534", "date");
mutiple times for different results, how can I return both name & date from that function, then echo it out so I only have to use the开发者_如何学运维 function once?
function lookup($data, $what)
{
$json = file_get_contents("http://site.com/".$data.".json");
$output = json_decode($json, true);
echo $output['foo'][''.$what.''];
}
Thanks!
Have the function return the whole JSON result:
function lookup($data)
{
$json = file_get_contents("http://site.com/".$data.".json");
$output = json_decode($json, true);
return $output["foo"];
}
then you can do
$result = lookup("354534");
echo $result["name"];
echo $result["date"];
This is really a good idea, because otherwise, you'll make the (slow) file_get_contents request multiple times, something you want to avoid.
substitue echo
in your function with return
, and return the entire array:
function lookup($data){
...
return $output;
}
...
$results = lookup($someData);
echo $results['name'] . $results['date'];
精彩评论