0

This is Data recovery from $_POST

$list = 
array(0=>$_POST['matricule'],1=>$_POST['nom'],2=>$_POST['prenom'],3=>$_POST['dn'],4=>$_POST['tel'],5=>$_POST['mail'],6=>$_POST['sexe'],7=>$_POST['adresse']);                       
$contenu_json = json_encode($list);
fwrite($fp, $contenu_json); // "test.json"

And this Example result in test.json :

 ["123","test","test","test","test","test","test","test"]["2","test","test","test","","","",""]

Sample code 2:

$json_source = file_get_contents("test.json");
$data = json_decode($json_source, true);
echo '$data[1]';

Output $data[0] is "" :( . i want to see "123"

My objectif is to see all data json in form like :
    <input name="id" type="text" value="<?= $data[0] ?>" >

what's the problem?

  • What is shown is not valid json. Paste what's in the file into a json validator to confirm – charlietfl Oct 14 '18 at 12:48
  • I need to read all data in the json file,but when i did '$date[0]'; i see nothing – Yawmeyyet TOunsi Oct 14 '18 at 13:10
  • Did you validate your file as mentioned? – charlietfl Oct 14 '18 at 13:12
  • i don't understand you, my objectif is to see all data json in form like : but $date[0] i see nothing , i would see "123" for example – Yawmeyyet TOunsi Oct 14 '18 at 13:25
  • Follow the first comment I made. – charlietfl Oct 14 '18 at 13:28
  • i understand, so how to make it valid? it generated automatique `$list = array(0=>$_POST['matricule'],1=>$_POST['nom'],2=>$_POST['prenom'],3=>$_POST['dn'],4=>$_POST['tel']); $contenu_json = json_encode($list); fwrite($fp, $contenu_json); ` – Yawmeyyet TOunsi Oct 14 '18 at 13:32
  • You need an outer array for all the inner arrays produced by that code. `[][]` is not a valid structure but `[[],[]]` is – charlietfl Oct 14 '18 at 13:34
  • ah thank you i understand, so you have a solution how to change this code? to be [[],[]] and not [][] when i execute it two times :) : `$list = array(0=>$_POST['matricule'],1=>$_POST['nom'] ...); $contenu_json = json_encode($list); fwrite($fp, $contenu_json); ` – Yawmeyyet TOunsi Oct 14 '18 at 13:38
  • Set file up to contain one empty json array. Read whole file and decode it. Push new sub array and encode whole master array to print whole file. Using database would be simpler also – charlietfl Oct 14 '18 at 13:40
  • Thank you :D @charlietfl – Yawmeyyet TOunsi Oct 14 '18 at 16:16

0 Answers0