Answer the question
In order to leave comments, you need to log in
How to update JSON using php?
Can you tell me how to update JSON using php? Let's say there are 100 entries, example:
[
{
"id": 1000,
"date": "2020-07-22 18:57:51",
"customer": "Hilary Greer",
},
{
"id": 1001,
"date": "2020-04-19 03:39:30",
"customer": "Yen Ortega",
},
{
"id": 1002,
"date": "2019-07-03 15:44:11",
"customer": "Maris Oconnor",
},
........
]
$jsonOrders = file_get_contents('templates/api/index.html.twig');
$orders = json_decode($jsonOrders);
foreach($orders as $order)
{
if($order->id == 1002)
{
$order->customer = 'updated';
}
}
file_put_contents('templates/api/index.html.twig', json_encode($orders)); // Перекодировать в формат и записать в файл.
Answer the question
In order to leave comments, you need to log in
$jsonOrders = file_get_contents('templates/api/index.html.twig');
$orders = json_decode($jsonOrders); //,TRUE
foreach($orders as $order)
{
if($order->id == 1002)
{
$order->status = 'cancelled';
}
}
file_put_contents('templates/api/index.html.twig', json_encode($orders));
$jsonOrders = file_get_contents('orders.json');
$items = json_decode($jsonOrders,true);
$items2 = array();
foreach($items as $item) {
if($item['id'] == '1002')
$item['customer'] = 'value';
array_push($items2, $item);
}
file_put_contents('orders.json', json_encode($items2));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question