我需要使用PHP foreach循环来格式化JSON数组的所有date_of_birth属性。
初始日期为Y-m-d,我只需要使用PHP将其格式化为d-m-Y。
我已经尝试过了,但是也许我做的不对。
<?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"
}
]
答案 0 :(得分:2)
由于某种原因,您检查“ 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"
}
]
答案 1 :(得分:0)
尝试更改此行:
$oDate = DateTime::createFromFormat('Y-m-d', '1988-08-21');
有了这个:
$oDate = DateTime::createFromFormat('Y-m-d', $value['date_of_birth']);
答案 2 :(得分:0)
您必须使用strtotime():
for($i=0;$i<count($characters);$i++) {
$characters[$i]['date_of_birth'] = date("d-m-Y", strtotime($characters[$i]['date_of_birth']));
}
此处日期格式更新为d-m-y
答案 3 :(得分:0)
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)