用数组发送几个返回。如何?

时间:2011-07-20 07:06:59

标签: php arrays codeigniter

我想要这个代码发送给AJAX CALL的结果,这有两个结果。必须发送数组吗?怎么回事?

现在返回的是:'1'

$date_go = '1111/11/11';
$date_back = '1390/00/05';

$jdate = jgmdate("Y/m/j");
list($year_now, $month_now, $day_now) = explode('/', $jdate, 3);

list($year_back, $month_back, $day_back) = explode('/', $date_back, 3);
list($year_go, $month_go, $day_go) = explode('/', $date_go, 3);            
if($year_go>=$year_now && $month_go<=12 && $year_back>=$year_now && $month_back<=12) {

    $j2g_back = $this->convert_date->JalaliToGregorian($year_back, $month_back, $day_back);
    return array($j2g_back[0]."/".$j2g_back[1]."/".$j2g_back[2]);

    $j2g_go = $this->convert_date->JalaliToGregorian($year_go, $month_go, $day_go);
    return array($j2g_go[0]."/".$j2g_go[1]."/".$j2g_go[2]);

2 个答案:

答案 0 :(得分:1)

您正在返回一个只包含1个元素的数组,请尝试这样:

$my_array = array();
if($year_go>=$year_now && $month_go<=12 && $year_back>=$year_now && $month_back<=12) 
{
    $j2g_back = $this->convert_date->JalaliToGregorian($year_back, $month_back, $day_back);
    $my_array[] = $j2g_back[0]."/".$j2g_back[1]."/".$j2g_back[2];
    $j2g_go = $this->convert_date->JalaliToGregorian($year_go, $month_go, $day_go);
    $my_array[] = $j2g_go[0]."/".$j2g_go[1]."/".$j2g_go[2];
}
return json_encode($my_array);

这样,您可以向数组添加2个元素,并返回JSON编码的数组。

答案 1 :(得分:0)

返回仅适用于函数。这段代码是否包含在函数中?

如果您希望返回两个数组,则必须将它们添加到单个数组并调用json_encode