2017-04-27 122 views
0

如何,這是我的示例JSON數據到JSON數據存儲到MySQL和PHP

[ 
    {"kode":"AX5","harga":"6200","status":"1","nama":"AXIS 5"}, 
    {"kode":"AX10","harga":"11250","status":"1","nama":"AXIS 10"}, 
    {"kode":"AX25","harga":"25750","status":"1","nama":"AXIS 25"}, 
    {"kode":"AX50","harga":"50800","status":"1","nama":"AXIS 50"} 
] 

,我想將數據保存到使用PHP,現場的product_id,價格,狀態,名稱mysql的,任何人都可以幫助我請

我的問題是,我不知道對我來說更好的代碼在PHP

+1

顯示您已經有其他的數據存儲到數據庫 – RiggsFolly

回答

3

你可以使用PHP

json_decode() 

函數將該json字符串轉換爲PHP變量。

然後,您可以獲取這些值並將它們保存到MySQL數據庫;

來源json_decode PHP Manual

1

可以使用json_decode()。它需要一個JSON編碼的字符串並將其轉換爲一個PHP變量。

<?php 
$json_data = '[{"kode":"AX5","harga":"6200","status":"1","nama":"AXIS 5"},{"kode":"AX10","harga":"11250","status":"1","nama":"AXIS 10"},{"kode":"AX25","harga":"25750","status":"1","nama":"AXIS 25"},{"kode":"AX50","harga":"50800","status":"1","nama":"AXIS 50"}]'; 
$array_data = json_decode($json_data); 
echo '<pre>'; 
print_r($array_data); 

foreach ($array_data as $event) { 
    echo 'Product_id:' . $event->kode; 
    echo "<br>"; 
    echo 'status:' . $event->status; 
    echo "<br>"; 
} 

然後輸出

Array 
(
    [0] => stdClass Object 
     (
      [kode] => AX5 
      [harga] => 6200 
      [status] => 1 
      [nama] => AXIS 5 
     ) 

    [1] => stdClass Object 
     (
      [kode] => AX10 
      [harga] => 11250 
      [status] => 1 
      [nama] => AXIS 10 
     ) 

    [2] => stdClass Object 
     (
      [kode] => AX25 
      [harga] => 25750 
      [status] => 1 
      [nama] => AXIS 25 
     ) 

    [3] => stdClass Object 
     (
      [kode] => AX50 
      [harga] => 50800 
      [status] => 1 
      [nama] => AXIS 50 
     ) 

) 

Product_id:AX5 
status:1 
Product_id:AX10 
status:1 
Product_id:AX25 
status:1 
Product_id:AX50 
status:1 

瞭解更多信息

http://php.net/manual/en/function.json-decode.php

+0

代碼檢查,你看我的更新答案數據 –

+0

Thankyouuu非常,工作 –

+0

你能接受我的答案嗎? –