【问题标题】:loop data inside an array [duplicate]在数组中循环数据[重复]
【发布时间】:2018-07-28 16:46:44
【问题描述】:

如何将值存储在数组中的循环中并在 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;

【问题讨论】:

  • $array[] = $value
  • 请注意,使用 1 个月作为间隔可能会有问题。见example 3

标签: php yii2


【解决方案1】:

我想这就是你需要的

 $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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2016-04-14
    • 1970-01-01
    • 2017-09-16
    • 2012-03-03
    • 2021-10-11
    相关资源
    最近更新 更多