循环数组内的数据

时间:2018-02-18 07:28:41

标签: php yii2

如何将值存储在数组中的循环内,并在yii2中以JSON形式返回。

我对此代码有疑问:

$start = date("Y/m/d");
            $interval = DateInterval::createFromDateString('1 month');
            $end = date('Y/m/d', strtotime(sprintf("+%d months", $loanMonths)));
            $periods = new DatePeriod(new DateTime($start), $interval, new DateTime($end));
            $array = array();
            foreach($periods as $period){
                print_r(date('Y-F', strtotime($period->format('Y-m')))); echo "<br>";
            }
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $period;

1 个答案:

答案 0 :(得分:0)

我认为这就是你需要的

 $array = array();
 foreach($periods as $period){
    $array []= date('Y-F', strtotime($period->format('Y-m')));
 }

 $response = Yii::$app->response;
 $response->format = \yii\web\Response::FORMAT_JSON;
 $response->data = $array;

 return $response;