[英]How do I format JSON date array object using PHP
我需要使用PHP foreach循環來格式化JSON數組的所有date_of_birth屬性。
初始日期為Ymd,我只需要使用PHP將其格式化為dmY。
我已經嘗試過了,但是也許我做的不對。
<?php
// Convert json from objects to array
$characters = json_decode(file_get_contents('data.json'), true);
//Loop through array
foreach ($characters as $key => $value) {
if (in_array($key, ['date_of_birth'])) {
$oDate = DateTime::createFromFormat('Y-m-d', '1988-08-21');
$characters[$key]['date_of_birth'] = $oDate->format('d-m-Y');
}
}
file_put_contents('results_new.json', json_encode($characters));
print_r($characters);
?>
//JSON data
[
{
"first_name" : "Andy",
"last_name" : "James",
"date_of_birth" : "1988-08-21",
"date_of_move" : "2000-09-11"
},
{
"first_name" : "Laura",
"last_name" : "Simmons",
"date_of_birth" : "1968-04-09",
"date_of_move" : "2010-09-05"
},
{
"first_name" : "Jeff",
"last_name" : "Bridge",
"date_of_birth" : "1980-02-15",
"date_of_move" : "1990-08-08"
}
]
由於某種原因,您檢查“ date_of_birth”鍵的方式會引起問題。 我設法使所有值都使用$ value和引用在foreach()循環中工作。 請嘗試以下方法:
<?php
// Convert json from objects to array
$characters = json_decode(file_get_contents('data.json'), true);
//Loop through array
foreach ($characters as $key => &$value) {
if (array_key_exists('date_of_birth', $value)) {
$oDate = DateTime::createFromFormat('Y-m-d', $value['date_of_birth']);
$value['date_of_birth'] = $oDate->format('d-m-Y');
}
}
file_put_contents('results_new.json', json_encode($characters));
print_r($characters);
輸出:
[
{
"first_name": "Andy",
"last_name": "James",
"date_of_birth": "21-08-1988",
"date_of_move": "2000-09-11"
},
{
"first_name": "Laura",
"last_name": "Simmons",
"date_of_birth": "09-04-1968",
"date_of_move": "2010-09-05"
},
{
"first_name": "Jeff",
"last_name": "Bridge",
"date_of_birth": "15-02-1980",
"date_of_move": "1990-08-08"
}
]
嘗試更改此行:
$oDate = DateTime::createFromFormat('Y-m-d', '1988-08-21');
有了這個:
$oDate = DateTime::createFromFormat('Y-m-d', $value['date_of_birth']);
您必須使用strtotime():
for($i=0;$i<count($characters);$i++) {
$characters[$i]['date_of_birth'] = date("d-m-Y", strtotime($characters[$i]['date_of_birth']));
}
日期格式更新為dmy
in_array($key, ['date_of_birth'])
無法正常工作。 嘗試這種方式:
$json = '[
{
"first_name" : "Andy",
"last_name" : "James",
"date_of_birth" : "1988-08-21",
"date_of_move" : "2000-09-11"
},
{
"first_name" : "NO DATE",
"last_name" : "NO DATE"
},
{
"first_name" : "Laura",
"last_name" : "Simmons",
"date_of_birth" : "1968-04-09",
"date_of_move" : "2010-09-05"
},
{
"first_name" : "Jeff",
"last_name" : "Bridge",
"date_of_birth" : "1980-02-15",
"date_of_move" : "1990-08-08"
}]';
$decoded = json_decode($json);
foreach ($decoded as $key => $value)
{
if (isset($value->date_of_birth))
{
$value->date_of_birth = DateTime::createFromFormat('Y-m-d', $value->date_of_birth)->format('d-m-Y');
}
}
var_dump($decoded);
輸出:
array (size=4)
0 =>
object(stdClass)[1]
public 'first_name' => string 'Andy' (length=4)
public 'last_name' => string 'James' (length=5)
public 'date_of_birth' => string '21-08-1988' (length=10)
public 'date_of_move' => string '2000-09-11' (length=10)
1 =>
object(stdClass)[2]
public 'first_name' => string 'NO DATE' (length=7)
public 'last_name' => string 'NO DATE' (length=7)
2 =>
object(stdClass)[3]
public 'first_name' => string 'Laura' (length=5)
public 'last_name' => string 'Simmons' (length=7)
public 'date_of_birth' => string '09-04-1968' (length=10)
public 'date_of_move' => string '2010-09-05' (length=10)
3 =>
object(stdClass)[4]
public 'first_name' => string 'Jeff' (length=4)
public 'last_name' => string 'Bridge' (length=6)
public 'date_of_birth' => string '15-02-1980' (length=10)
public 'date_of_move' => string '1990-08-08' (length=10)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.