私はかなりPHP、JSONなどに新しいです。 HTML5フォームをJSONデータベースに入力し、その情報を呼び出すという課題があります。end()は、パラメータ1が配列であることを予期しています。
<?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));
?>
とこれがそうIDが自動インクリメントする必要がありますが、私はこのエラーを取得することをしようとすると、出力
[{"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}]
です:この部分
を指しend() expects parameter 1 to be array, null given
$last_item = end($data);
$last_item_id = $last_item['id'];
タイプミス 'json_decode(真$ファイルを、)支援を期待;' – cske
チェック '$のdata'は' IS_ARRAY($データ) ' –
どうやら' json_decode() ''失敗し、返しを使って配列の場合偽です。 – arkascha