如何使用Angular 4中的表单用PHP更新json文件?

时间:2017-12-03 00:20:48

标签: php json angular

到目前为止,我已经设置了表单和服务来读取应该编辑json文件的php文件。我当前的PHP文件看起来像这样。

 if(empty($errors))
            {         
            $postdata = file_get_contents("php://input");
            $request = json_decode($postdata);       
            $name = $request->name;
            $designation = $request->designation;

            $current_data = file_get_contents('currency.json');  
            $array_data = json_decode($current_data, true);  
            $extra = array(  
                  'name'            =>     $name,  

                  'designation'     =>     $designation  
            );  
            $array_data[] = $extra;  
            $final_data = json_encode($array_data);  
            if(file_put_contents('currency.json', $final_data))  
            {  
                  $message = "<label class='text-success'>File Appended Success fully</p>";  
            }  
           }  
           else  
           {  
                $error = 'JSON File not exits';  
           }  `enter code here`

I will appreciate any effort for help.

1 个答案:

答案 0 :(得分:0)

好吧,我花了12个小时的时间,我有一个解决方案:

       if(file_exists('currency.json'))  
       {  
        $jsonString = file_get_contents("php://input");

        date_default_timezone_set("Europe/Skopje");
        $todayDate= date("d.m.Y");

        $data = json_decode($jsonString);

        $eur=array(
              'currency' => 'EUR',
              'buy' => (double)$data->eurBuy,
              'sell' => (double)$data->eurSell,
              'flag' => "flag--europe"
        );
        $aud=array(
              'currency' => 'AUD',
              'buy' => (double)$data->audBuy,
              'sell' => (double)$data->audSell,
              'flag' => "flag--australia"
              );

        $currencies=array($eur, $aud);

        $obj = array('datum' => $todayDate,'values' => $currencies);

        $newJsonString = json_encode($obj);
        file_put_contents('currency.json', $newJsonString);

}

希望很快我会放置演示项目,这样我可以在将来更精确地帮助开发人员。迎接!