Php end()要求参数1为数组,给定null

Php end()要求参数1为数组,给定null,php,json,Php,Json,好吧,我对PHP、JSON之类的东西还比较陌生。我被指派将HTML5表单输入JSON数据库,并回忆这些信息 <?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

好吧,我对PHP、JSON之类的东西还比较陌生。我被指派将HTML5表单输入JSON数据库,并回忆这些信息

<?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应该是自动递增的,但当我尝试时,我得到以下错误:

end() expects parameter 1 to be array, null given
指的是这一部分

 $last_item    = end($data);
 $last_item_id = $last_item['id'];

如果要获取数组的最后一个值(假设这是json数据),必须首先使用json_decode

$data = '[{"f_name":"Jack","l_name":"Smith","u_email":"jacksmith@hotmail.com","u_adress":"Something 1","u_postcode":"1111 AA","u_place":"SomeCity","u_birth":"jjjj-mm-dd","id":"1"}]';
因为解码时,它仍然在一个数组下,但只有一个索引,所以获取该索引并尝试以下操作:

$new_data = json_decode($data, true);
$last_item_id = end($new_data);
$id = $last_item_id = $last_item_id["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' => $id + 1
);
替换此行:

$data = json_decode('data.json', true);
与:

并且不需要再次提取id。删除:

$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
    );
请注意,您没有将数据保存在正确的json 1D数组中,如果使用
$data[]=array(..)
如果要保留,请更换:

$last_item = end($data);
与:

长话短说,工作代码可以是:

$data = json_decode($file, true);
$last_item = end($data[0]);
 $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
        );

这取决于你想要什么。
我希望它有助于

typo
json\u解码($file,true)
检查
$data
是否是一个数组,使用
is\u array($data)
显然
json\u decode()
失败并返回
false
。尝试使用
$data=json\u decode($file,true)
Yeah@Mike…当我阅读答案并发布评论时,其他未反映的评论…根据代码,我认为OP需要json文件中最后一项的ID,而不是第一项。编辑了我的答案,先生,似乎不仅仅是OP的问题,end()抛出了一个错误。
$new_data[0]
获取第一个元素,而不是最后一个元素。如果数据按我提供的方式提供,则它不应是第一个元素。数据可能不会按提供的方式提供。对于所提供的数据,这样做的唯一原因是数组中只有一个元素,因此第一个元素与最后一个元素相同。最后一部分是错误的。看看json的数据结构,它是一个对象数组。我已经编辑了我的答案。您应该使用$data而不是$data[]将最终分析数据保存到文件中。因为您当前没有在文件中保存json 1D数组,所以您正在保存json 2D数组。如果要继续保存为2D数组,那么ypu需要使用:$last_item=end($data[0]);在提取id时。感谢所有快速响应!虽然错误本身已经修复,但我仍然在提取ID并增加它时遇到问题。现在它只显示一个空id。
$last_item = end($data);
$last_item = end($data[0]);
$data = json_decode($file, true);
$last_item = end($data[0]);
 $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
        );
$data = json_decode($file, true);
$last_item = end($data);
 $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
        );