I have the following function in a class:
public function getDomains($RESELLER)
{
$data['object'] = 'domain';
$data['action'] = 'read';
$data['territory'] = $RESELLER;
$data['format'] = 'json';
if ($response = $this->request($data)) {
$obj = json_decode($response, true);
foreach ($obj as $file) {
return $file['domain'];
}
}
}
I am using the following:
$Domains = $api->getDomains($RESELLER);
echo $Domains."<br>";
If I change the return to echo in the function, it will print all the domains. But if I leave it as it is, I only get the one value. I tried doing a foreach on the $Domains for the return but I ended up with an error.
My end result is I want to be able to take my returned value and pass it to another function so I can leave this function with just one job without making it into a function doing more then just one job.
return
terminates a function and returns the value. One solution could be to collect all the domains to an array and then return it:
public function getDomains($RESELLER)
{
$data['object'] = 'domain';
$data['action'] = 'read';
$data['territory'] = $RESELLER;
$data['format'] = 'json';
$retval = array(); # Create the array
if ($response = $this->request($data)) {
$obj = json_decode($response, true);
foreach ($obj as $file) {
$retval[] = $file['domain']; # Add values to it
}
}
return $retval; # Return it
}
Return will return from the function when its hit, hence why your only getting a single row,
Store the result in an array and then return:
public function getDomains($RESELLER)
{
$data['object'] = 'domain';
$data['action'] = 'read';
$data['territory'] = $RESELLER;
$data['format'] = 'json';
$data['domain'] = array();
if ($response = $this->request($data)) {
$obj = json_decode($response, true);
foreach ($obj as $file) {
$data['domain'][] = $file['domain'];
}
}
}
return $data;
}