返回foreach结果

时间:2015-01-26 08:39:12

标签: php

我能够在foreach循环中从yahoo API中打印_r数据。但是,我无法在函数的最后一行返回它。

function callcontact_yahoo($consumer_key, $consumer_secret, $guid, $access_token, $access_token_secret, $usePost=false, $passOAuthInHeader=true)
{
  $retarr = array();  // return value
  $response = array();

    $yahoo_array = array();
    $newList = "";
    // extract successful response
        if (! empty($response)) 
            {
                list($info, $header, $body) = $response;
                    if ($body) 
                        {
                            $yahoo_array = json_decode($body);

                            echo "<pre/>";
                                foreach($yahoo_array as $key=>$values)
                                    {                
                                        foreach($values->contact as $keys=>$values_sub)
                                            {
                                                $fields = $values_sub->fields;
                                                    foreach($values_sub->fields as $field)
                                                        {
                                                            $convers = $field->value;
                                                        }
                                                $oneemail   = $convers.",";
                                                //print_r($oneemail);
                                                //echo "</br>";
                                                echo "I am one email:  $oneemail </br>";
                                            } 
                                    }
                        }
            }
  return $retarr;
}

echo "I am one email: $oneemail </br>";的行中,我可以看到列出的所有电子邮件联系人。但是如何在return $retarr;中返回它请帮忙。我试图调试它,但仍然没有想到。

1 个答案:

答案 0 :(得分:0)

你这样说:

foreach($values->contact as $keys=>$values_sub)
{
    $fields = $values_sub->fields;
    foreach($values_sub->fields as $field)
    {
    $convers = $field->value;
    }
    $oneemail   = $convers.",";
    //print_r($oneemail);
    //echo "</br>";
    echo "I am one email:  $oneemail </br>";
    $retarr[] = $oneemail;
}