Janneck Lange
Janneck Lange

Reputation: 932

edit JSON Objects in JSON Objecs with PHP

How can i increment x or y in this JSON file? I posted my PHP code to increment the counter, but i dont know how to acces the "infos" -> "x" or "y".

JSON file

{
  "counter": 4,
  "infos": {
    "x": 1,
    "y": 2
  }
}

PHP file

<?php
$contents = file_get_contents('../test.json');
$contentsDecoded = json_decode($contents, true);

$contentsDecoded['counter']++;

$json = json_encode($contentsDecoded);
file_put_contents('../test.json', $json);
?>

Upvotes: 1

Views: 48

Answers (1)

sensorario
sensorario

Reputation: 21668

Just use

$decoded['infos']['x']++;

Upvotes: 1

Related Questions