2017-04-25 55 views
-1

排除數據我有此JSON文件,它來自URL從陣列

{ 
"eur": { 
    "code": "EUR", 
    "alphaCode": "EUR", 
    "numericCode": "978", 
    "name": "Euro", 
    "rate": 0.92014592622619, 
    "inverseRate": 1.0867841409692, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
}, 
"tnd": { 
    "code": "TND", 
    "alphaCode": "TND", 
    "numericCode": "788", 
    "name": "Tunisian Dinar", 
    "rate": 2.5199297392244, 
    "inverseRate": 0.39683646112601, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
}, 
"try": { 
    "code": "TRY", 
    "alphaCode": "TRY", 
    "numericCode": "949", 
    "name": "Turkish Lira", 
    "rate": 3.5735711390353, 
    "inverseRate": 0.27983212341198, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
}, 
"gbp": { 
    "code": "GBP", 
    "alphaCode": "GBP", 
    "numericCode": "826", 
    "name": "U.K. Pound Sterling", 
    "rate": 0.78192136197811, 
    "inverseRate": 1.2789009849663, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
}, 
"cad": { 
    "code": "CAD", 
    "alphaCode": "CAD", 
    "numericCode": "124", 
    "name": "Canadian Dollar", 
    "rate": 1.3511687609782, 
    "inverseRate": 0.7401, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
}, 
"jod": { 
    "code": "JOD", 
    "alphaCode": "JOD", 
    "numericCode": "400", 
    "name": "Jordanian Dinar", 
    "rate": 0.70943909789052, 
    "inverseRate": 1.409564264182, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
}, 
"egp": { 
    "code": "EGP", 
    "alphaCode": "EGP", 
    "numericCode": "818", 
    "name": "Egyptian Pound", 
    "rate": 18.190428713858, 
    "inverseRate": 0.054973965469992, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
}, 
"ils": { 
    "code": "ILS", 
    "alphaCode": "ILS", 
    "numericCode": "376", 
    "name": "Israeli New Sheqel", 
    "rate": 3.649, 
    "inverseRate": 0.27404768429707, 
    "date": "Mon, 24 Apr 2017 20:30:00 GMT" 
} 

}

我犯了一個PHP頁面插入陣列到數據庫中,這裏是代碼:

<?php 
     $connect = mysqli_connect("localhost", "tabs", "pass", "tabs"); //Connect PHP to MySQL Database 
     $filename = "usd.json"; 
     $data = file_get_contents($filename); //Read the JSON file in PHP 
     $array = json_decode($data, true); 

     foreach($array as $row) //Extract the Array Values by using Foreach Loop 
     {  
     $query .= "INSERT INTO PrBlack(code, ncode, rate, date) VALUES ('".$row["code"]."', '".$row["numericCode"]."', '".$row["rate"]."', '".$row["date"]."'); "; // Make Multiple Insert Query 

     } 
     if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query 
{ 
echo '<h3>Imported Data</h3><br />'; 

     } 

     ?> 

它工作正常,但如果我只想刪除「eur」數組呢?我該怎麼辦?

+1

$ array = json_decode($ data,true);爲什麼你不像unset($ array [「eur」])那樣設置「eur」? –

+0

它的工作原理!謝謝 – egaidi

回答

1

正好被清除($陣列[ 「EUR」])解碼後的數組:

<?php 
     $connect = mysqli_connect("localhost", "tabs", "pass", "tabs"); //Connect PHP to MySQL Database 
     $filename = "usd.json"; 
     $data = file_get_contents($filename); //Read the JSON file in PHP 
     $array = json_decode($data, true); 
     unset($array["eur"]); 
     foreach($array as $row) //Extract the Array Values by using Foreach Loop 
     {  
     $query .= "INSERT INTO PrBlack(code, ncode, rate, date) VALUES ('".$row["code"]."', '".$row["numericCode"]."', '".$row["rate"]."', '".$row["date"]."'); "; // Make Multiple Insert Query 

     } 
     if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query 
{ 
echo '<h3>Imported Data</h3><br />'; 

     } 

     ?> 
+0

是的,它的工作原理!謝謝 – egaidi

+0

如果它對你有用,所以希望你能接受這個答案通過打鉤作爲正確的答案 –

+0

修改原始輸入是一個不好的做法。最好忽略不相關的事情。 – modsfabio