从数组中排除数据

时间:2017-04-25 12:43:19

标签: php arrays json

我有这个来自URL的JSON文件

{
"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”数组怎么办?我该怎么办?

2 个答案:

答案 0 :(得分:1)

foreach($array as $currency=>$row)
{
  if($currency == "eur") continue;

  //DO SOMETHING WITH THE REST
}

手动:http://php.net/manual/en/control-structures.foreach.php

  

foreach(array_expression as $ key =&gt; $ value)

答案 1 :(得分:1)

解码数组后只需取消设置($ array [“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 />';

      }

      ?>