Auto increment id JSON



PHP Snippet 1:

$idsList = array_column($data, 'id');

PHP Snippet 2:

$auto_increment_id = max($idsList) + 1;

PHP Snippet 3:

$file = file_get_contents("data.json");
$data = json_decode($file, true);

// Get IDs list
$idsList = array_column($data, 'id');
// Get unique id
$auto_increment_id = max($idsList) + 1;

$data[] = array(
    'id'             => $auto_increment_id,
    'title'          => $_POST["title"],
    'artist'         => $_POST["artist"],
    'genre'          => $_POST["genre"],
    'week'           => $_POST["week"],
    'highest_rating' => $_POST["highest_rating"],
    'year'           => $_POST["year"],
    'youtube'        => $_POST["youtube"],
    "links"          => [ 
        array(
            "rel"=>"self",
            "href"=>""
        ),
        array(
            "rel"=>"collection",
            "href"=>""
        )
    ]
);

file_put_contents('data.json',json_encode($data), LOCK_EX);

PHP Snippet 4:

    "id": "2",
    "title": "Hello World",
    "artist": "John Smith"
    ...

PHP Snippet 5:

    "id": 2,
    "title": "Hello World",
    "artist": "John Smith"
    ...