In Ordnung, also bin ich ziemlich neu in PHP, JSON und dergleichen. Ich bekam eine Aufgabe, HTML5-Formular in eine JSON-Datenbank einzugeben und diese Information ebenfalls aufzurufen.end() erwartet, dass Parameter 1 Array ist, null gegeben
<?php
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$u_email = $_POST['u_email'];
$u_adress = $_POST['u_adress'];
$u_postcode = $_POST['u_postcode'];
$u_place = $_POST['u_place'];
$u_birth = $_POST['u_birth'];
$file = file_get_contents("data.json");
$data = json_decode('data.json', true);
$last_item = end($data);
$last_item_id = $last_item['id'];
$data[] = array(
'f_name'=>$f_name,
'l_name'=>$l_name,
'u_email'=>$u_email,
'u_adress' =>$u_adress,
'u_postcode' =>$u_postcode,
'u_place'=>$u_place,
'u_birth'=>$u_birth,
'id' =>++$last_item_id
);
file_put_contents('data.json', json_encode($data));
?>
und dies ist der Ausgang
[{"f_name":"Jack","l_name":"Smith","u_email":"[email protected]","u_adress":"Something 1","u_postcode":"1111 AA","u_place":"SomeCity","u_birth":"jjjj-mm-dd","id":1}]
so die Identifikation der Autoinkrement sein sollte, aber wenn ich versuche, dass ich diesen Fehler:
end() expects parameter 1 to be array, null given
, die in diesem Teil bezieht sich
$last_item = end($data);
$last_item_id = $last_item['id'];
Typo 'json_decode ($ file, true) hilft,' – cske
überprüfen, ob '$ data' ist ein Array' is_array ($ data) ' –
Offenbar' json_decode() 'nicht und kehrt' mit falsch ". – arkascha