php使用foreach返回数据时新增key, value

第一次尝试,在foreach时,根据$row['season']值增加label值

public function getNavItem(Request $request){
        $arr = TasksData::selectRaw('year, season')
            ->distinct()
            ->orderBy('season', 'DESC')
            ->orderBy('year', 'ASC')
            ->get()
            ->toArray();

        $collection = collect($arr);
        $grouped = $collection->groupBy('year');
        $grouped->toArray();
		$result = [];
        foreach ($grouped as $key => $value) {
            $year['year'] = $key;
            foreach ($value as $row) {
                if($row['season']=='1') {
                    $season[]['value'] = $row['season'];
                    $season[]['label'] = '1-3月';
                }
                if($row['season']=='2') {
                    $season[]['value'] = $row['season'];
                    $season[]['label'] = '4-6月';
                }
                if($row['season']=='3') {
                    $season[]['value'] = $row['season'];
                    $season[]['label'] = '7-9月';
                }
                if($row['season']=='4') {
                    $season[]['value'] = $row['season'];
                    $season[]['label'] = '10-12月';
                }

            }
            $year['season'] =$season;
            $result[] =$year;
        }

        return $result;
}

返回的数据格式不是目标格式

{
	season: [
		0: {value: "4"},
		1: {label: "10-12月"},
		2: {value: "3"},
		3: {label: "7-9月"},
		4: {value: "2"},
		5: {label: "4-6月"},
		6: {value: "1"},
		7: {label: "1-3月"}
	],
	year: 2018
}

第二次尝试,

 /*
     * 功能:返回季节的中文名
     * 1.$season_id:传入的数字型字符串
     * 2.$chinese: 季节的字符串
    */
    public function  mappingSeason($season_id) {
        switch ($season_id){
            case "1":
                $chinese = "1-3月";
                break;
            case "2":
                $chinese = "4-6月";
                break;
            case "3":
                $chinese = "7-9月";
                break;
            case "4":
                $chinese = "10-12月";
                break;
            default:
                $chinese = $season_id;
                break;
        }

        return $chinese;
    }

 public function getNavItem(Request $request){
        $arr = TasksData::selectRaw('year, season')
            ->distinct()
            ->orderBy('season', 'DESC')
            ->orderBy('year', 'ASC')
            ->get()
            ->toArray();


        $collection = collect($arr);
        $grouped = $collection->groupBy('year');
        $grouped->toArray();
        $result = [];
        $rowInResult = [];
        foreach ($grouped as $year => $arr1) {
            $rowInResult["season"] = [];
            $rowInResult["year"] = $year;

            foreach ($arr1 as $row) {
                $seaonInfo = [];
                $seaonInfo["value"] = $row["season"];
                
                $seaonInfo["label"] = $this->mappingSeason($row["season"]);

                $dateRangeArr = $this->getStartDateAndEndDate($year, $row["season"]);

                $seaonInfo["startDate"] = $dateRangeArr[0];
                $seaonInfo["endDate"] = $dateRangeArr[1];
                $rowInResult["season"][] = $seaonInfo;
            }

            $result[] = $rowInResult;
        }
        return $result;
        
    }

返回的数据格式满足预期

{
	season: [
		0: {value: "4", 
			label: "10-12月", 
			startDate: "2018-10-01", 
			endDate: "2018-12-31"
		},
		1: {
			value: "3", 
			label: "7-9月", 
			startDate: "2018-07-01", 
			endDate: "2018-09-30"
		},
		2: {
			value: "2", 
			label: "4-6月", 
			startDate: "2018-04-01", 
			endDate: "2018-06-30"
		},
		3: {
			value: "1", 
			label: "1-3月", 
			startDate: "2018-01-01", 
			endDate: "2018-03-31"
		}
	],
	year: 2018
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值