PHP数组到JSON - >添加值名称

时间:2016-12-05 04:59:06

标签: php

PHP Script Snippet(只有一个假期):

<?PHP
function calculateBankHolidays($yr) {

    $bankHols = Array();

// New year's:
switch ( date("w", strtotime("01-01-$yr 12:00:00")) ) {
    case 6:
        $bankHols[] = "03-01-$yr";
        break;
    case 0:
        $bankHols[] = "02-01-$yr";
        break;
    default:
        $bankHols[] = "01-01-$yr";
}

// Good friday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) - 2)." days", strtotime("21-03-$yr 12:00:00") ));

// Easter Monday:
$bankHols[] = date("d-m-y", strtotime( "+".(easter_days($yr) + 1)." days", strtotime("21-03-$yr 12:00:00") ));

// May Day:
    if ($yr == 1995) {
        $bankHols[] = "08-05-1995"; // VE day 50th anniversary year exception
    } else {
        switch (date("w", strtotime("01-05-$yr 12:00:00"))) {
            case 0:
                $bankHols[] = "02-05-$yr";
                break;
            case 1:
                $bankHols[] = "01-05-$yr";
                break;
            case 2:
                $bankHols[] = "07-05-$yr";
                break;
            case 3:
                $bankHols[] = "06-05-$yr";
                break;
            case 4:
                $bankHols[] = "05-05-$yr";
                break;
            case 5:
                $bankHols[] = "04-05-$yr";
                break;
            case 6:
                $bankHols[] = "03-05-$yr";
                break;
        }
    }

    return $bankHols;

}

header('Content-Type: application/json');

$bankHolsThisYear = calculateBankHolidays(2017);
echo (json_encode($bankHolsThisYear, JSON_PRETTY_PRINT));
?>

结果:

[
    "02-01-2017",
    "14-04-17",
    "17-04-17",
    "01-05-2017",
    "2017-05-29",
    "2017-08-28",
    "2017-12-25",
    "2017-12-26"
]

显示当前的完整脚本结果

期望的结果:

{
        "Holiday Name": {
                "Start Date": ,
                "End Date": ,
                "Holiday type": ,
                "Where it is observed": ,
        },

问题:

  1. 如何将“假日名称”添加到每个值的父级?
  2. 如何为每个当前值添加“开始日期”?

1 个答案:

答案 0 :(得分:1)

您可以创建一个多维关联对象数组,并执行以下操作:

    $listOfHolidays=array(
      'halloween'=>array('start'=>'10-31','end'=>'10-31','type'=>'trick or treat','celebratedBy'=>'childhood'),
      'newYear'=>array('start'=>'12-31','end'=>'01-01','type'=>'new year','celebratedBy'=>'everyone'),
);
    echo json_encode($listOfHolidays);

经过测试:这是我的输出:

{
    "halloween":
         {"start":"10-31",
           "end":"10-31",
           "type":"trick ortreat",
           "celebratedBy":"childhood"
         },
    "newYear":
        {"start":"12-31",
         "end":"01-01",
         "type":"new year",
         "celebratedBy":"everyone"
        }
  }
编辑:当你评论一个开关时,我不确定我是否理解它的精确度,但你可以很容易地得到假期&#39;通过使用关联键,如下所示:

$boo=$array['halloween'];

然后通过以下方式获取此假期的价值:

$boo['type']; //trick ortreat

或者您可以直接从原始数组中获取值:

echo $array['newYear']['end']; //01-01

您还可以向数组中添加一个值:

$array['newYear']['bonus']='300$';

另外,只是一个友好的提醒,你可以简单地通过使用json_decode中的TRUE开关从jason中重新调整aray:

$array=json_decode($json,true);

至于开关,我仍然不知道如何使用开关,除非你在假日中度过:

foreach($array as $k=>$v){
  switch($k){
   case 'halloween': echo $v['end']; break; //10-31
   case 'newYear': echo $v['bonus']; break; //300$
   default: echo 'normal work day'; break;
  }
}

希望这有帮助。