2016-11-07 121 views
-1

好吧,所以我相当新的PHP,JSON等。我得到了一个任务,将HTML5表单输入到JSON数据库中,并且还记得那些信息。结束()期望参数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)); 
?> 

,这是输出

[{"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}] 

所以ID应该是自动递增,但是当我尝试,我得到这个错误:

end() expects parameter 1 to be array, null given 

是指这部分

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

错字'json_decode($文件,真正的);' – cske

+0

检查是否'$ data'是使用'is_array($数据)数组' –

+0

显然'json_decode()'失败并返回'返回FALSE。 – arkascha

回答

0

替换此行:

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

与:

$data = json_decode($file, true); 

并且没有n再次提取id。删除:

$last_item_id = $last_item['id']; 

使用LAST_ITEM后:

$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的一维数组中的数据,你居然让2D如果你使用$data[] = array(....) 如果你想保留它然后更换:

$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 
     ); 

OR

$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 
     ); 

取决于你如何想。 我希望它能帮助

+0

最后一部分是错误的。看看json的数据结构,它是一个对象数组。 – jeroen

+0

jeroen已经说得很好。我编辑了我的答案。您应该使用$ data而不是$ data []将最终数据保存到文件中。因为目前你没有在文件中保存json一维数组,所以你正在保存json二维数组。如果你想保存为二维数组,那么ypu需要使用:$ last_item = end($ data [0]);同时提取ID。 – Learner

+0

感谢您的快速回复!虽然错误本身已经修复了,但是现在我仍然遇到了一个提取ID并增加它的问题。现在它只是显示一个空的ID。 – Soshiro

0

如果你wan t获得数组的最后一个值,因为这是一个json数据,你必须首先使用json_decode。

$data = '[{"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"}]'; 

因为当你解码它,但它仍然是一个数组下只是一个指标,获取指数和尝试这个办法:

$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 
); 
+0

根据代码,我会认为OP会想要json文件中最后一个项目的ID,而不是第一个。 – jeroen

+0

编辑我的答案先生,它似乎不只是OP的问题,结束()是抛出一个错误。 –

+1

'$ new_data [0]'获取第一个元素,而不是最后一个元素。 – Mike

相关问题