过滤、展平和重命名多维数组中的键 (Mayflower/Holidays)
Filtering, flattening and renaming keys in multidimensional array (Mayflower/Holidays)
我在 Laravel 7 中使用 Mayflower/Holidays 来获取我所在地区的假期列表。我得到的 Json 是:
{"2":{"type":"holiday","name":"My Holiday 1","weight":1,"date":"2020-05-01 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"5":{"type":"holiday","name":"My Holiday 2","weight":1,"date":"2020-05-20 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"}}
没错,但我需要不同的表示法。
[{"title":"My Holiday 1","start":"01.05.2020"},{"title":"My Holiday 2","start":"20.05.2020"}]
轻得多,一维,键 name
和 date
应该重命名为 title
和 start
。我试过
使用 foreach
和 array_map
的各种方法,但不断出错。使用 PHP 7 执行此操作的最佳方法是什么?
只需decode json string关联数组:
$array = json_decode($jsonString, true);
然后您可以使用 array_map
或 foreach
并根据您的表示格式化数据。
最后一步将是 converting back array to json string:
json_encode($jsonString);
既然你也标记了Laravel,我可以告诉你Laravel集合each()方法。
// your input
$jsonHolidays = '{"2":{"type":"holiday","name":"My Holiday 1","weight":1,"date":"2020-05-01 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"5":{"type":"holiday","name":"My Holiday 2","weight":1,"date":"2020-05-20 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"}}';
// convert json into array
$arrayHolidays = json_decode($jsonHolidays, true);
// Using laravel collection
$holidays = [];
collect($arrayHolidays)->each(function ($holiday) use (&$holidays) {
$holidays[] = [
'title' => $holiday['name'],
'start' => \Carbon\Carbon::parse($holiday['date'])->format('d.m.Y')
];
});
// converting back to json
return json_encode($holidays, true);
我在 Laravel 7 中使用 Mayflower/Holidays 来获取我所在地区的假期列表。我得到的 Json 是:
{"2":{"type":"holiday","name":"My Holiday 1","weight":1,"date":"2020-05-01 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"5":{"type":"holiday","name":"My Holiday 2","weight":1,"date":"2020-05-20 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"}}
没错,但我需要不同的表示法。
[{"title":"My Holiday 1","start":"01.05.2020"},{"title":"My Holiday 2","start":"20.05.2020"}]
轻得多,一维,键 name
和 date
应该重命名为 title
和 start
。我试过
使用 foreach
和 array_map
的各种方法,但不断出错。使用 PHP 7 执行此操作的最佳方法是什么?
只需decode json string关联数组:
$array = json_decode($jsonString, true);
然后您可以使用 array_map
或 foreach
并根据您的表示格式化数据。
最后一步将是 converting back array to json string:
json_encode($jsonString);
既然你也标记了Laravel,我可以告诉你Laravel集合each()方法。
// your input
$jsonHolidays = '{"2":{"type":"holiday","name":"My Holiday 1","weight":1,"date":"2020-05-01 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"5":{"type":"holiday","name":"My Holiday 2","weight":1,"date":"2020-05-20 00:00:00.000000","timezone_type":3,"timezone":"Europe\/Berlin"}}';
// convert json into array
$arrayHolidays = json_decode($jsonHolidays, true);
// Using laravel collection
$holidays = [];
collect($arrayHolidays)->each(function ($holiday) use (&$holidays) {
$holidays[] = [
'title' => $holiday['name'],
'start' => \Carbon\Carbon::parse($holiday['date'])->format('d.m.Y')
];
});
// converting back to json
return json_encode($holidays, true);