导入到mysql时删除json值中的逗号

我想通过php代码将json数据导入mysql。我有一个 json 文件,其中包含许多对象和字符串。


  "card": "2",

  "numbers": {

    "Conway": [234,872],

    "Fibonacci": [12]

  },

  "numbers2": {

    "Conway": [123,678],

    "Fibonacci": [12]

  }


}

和 php 文件


$query = '';

$table_data = '';

$filename = "urljsonfile.json";

$data = file_get_contents($filename); //Read the JSON file in PHP

$array = json_decode($data, true); //Convert JSON String into PHP Array

foreach($array as $row) //Extract the Array Values by using Foreach Loop

{

    $query .= "UPDATE tblname SET clumnname='".$array['numbers2']['Conway']."' WHERE id=2; ";  // Make Multiple Insert Query 

   $table_data .= '

            <tr>

                <td>'.$row["Conway"].'</td>

            </tr>

           '; //Data for display on Web page

}


if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query

{

    echo '<h3>Imported JSON Data</h3><br />';

    echo ' 

      <table class="table table-bordered">

          <tr>

              <th width="45%">Name</th>

          </tr>

     ';

    echo $table_data;  


    echo '</table>';

}

现在在 mysql 表中输出 123,678 我想像 123678 一样保存在 mysql 中


呼唤远方
浏览 174回答 3
3回答

动漫人物

尝试将两个数组值折叠为一个字符串,不带逗号。$numberString = implode('', $array['numbers2']['Conway']);

至尊宝的传说

你可以这样尝试(@Ro Achterberg 很接近......):$json = '{&nbsp;&nbsp;&nbsp; &nbsp;"card":"2",&nbsp; &nbsp;"numbers":{&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; "Conway":[&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;234,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;872&nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; "Fibonacci":[&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;12&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp;},&nbsp; &nbsp;"numbers2":{&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; "Conway":[&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;678&nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; "Fibonacci":[&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;12&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp;}}';$decodedJson = json_decode($json,true);$number = (int) implode("",$decodedJson["numbers2"]["Conway"]);&nbsp;var_dump($number); // Will output int(123678)作为$number = implode("",$decodedJson["numbers2"]["Conway"]);&nbsp;var_dump($number); // Will output string(6) "123678"&nbsp;编辑:使用您的代码和 $json 数组:$query = '';$table_data = '';$filename = "urljsonfile.json";$data = file_get_contents($filename); //Read the JSON file in PHP$array = json_decode($json, true); //Convert JSON String into PHP Arrayforeach($array as $row) //Extract the Array Values by using Foreach Loop{&nbsp; &nbsp; $query .= "UPDATE tblname SET clumnname='".implode("",$row["numbers2"]["Conway"])."' WHERE id=2; ";&nbsp; // Make Multiple Insert Query&nbsp;&nbsp; &nbsp;$table_data .= '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'.$row["Conway"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'; //Data for display on Web page}&nbsp; &nbsp; if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo '<h3>Imported JSON Data</h3><br />';&nbsp; &nbsp; &nbsp; &nbsp; echo '&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table class="table table-bordered">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th width="45%">Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;';&nbsp; &nbsp; &nbsp; &nbsp; echo $table_data;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo '</table>';&nbsp; &nbsp; }希望有帮助!OP 在他的评论中更改了 JSON,所以这应该有效:<?php&nbsp;$json = '{ "card":"2", "numbers":{ "Conway":"119,500", "Fibonacci":"12"}, "numbers2":{ "Conway":"123,678", "Fibonacci":"12" } }';$decodedJson = json_decode($json,true);&nbsp;$number = (int) str_replace("," , "" , $decodedJson["numbers"]["Conway"]);&nbsp;var_dump($number); // Will output int(119500)&nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP