douan2478 2017-04-06 09:43
浏览 118
已采纳

如何在php中将数组转换为mysql

Does anyone know how I can pull the currency (isoAlphaCode) and sellNote from the following array and add it to MySQL database?

Array
(
    [wrappedObject] => Array
        (
            [0] => Array
                (
                    [baseCurrency] => Array
                        (
                            [id] => 826
                            [description] => Great British Pound
                            [isoAlphaCode] => GBP
                        )

                    [fxCurrency] => Array
                        (
                            [id] => 978
                            [description] => Euro
                            [isoAlphaCode] => EUR
                        )

                    [buyNote] => 6.1
                    [sellNote] => 1.1495
                    [buyCheque] => 9.6
                    [sellCheque] => 
                    [rank] => HIGH
                    [denominations] => Array
                        (
                            [0] => 20
                            [1] => 50
                            [2] => 100
                            [3] => 200
                        )

                    [degradation] => 5
                    [upsellingDenomination] => 20
                    [collectionOrderDenominations] => 
                    [isExotic] => 
                )

            [1] => Array
                (
                    [baseCurrency] => Array
                        (
                            [id] => 826
                            [description] => Great British Pound
                            [isoAlphaCode] => GBP
                        )

                    [fxCurrency] => Array
                        (
                            [id] => 840
                            [description] => US Dollar
                            [isoAlphaCode] => USD
                        )

                    [buyNote] => 6
                    [sellNote] => 1.2268
                    [buyCheque] => 9.6
                    [sellCheque] => 
                    [rank] => HIGH
                    [denominations] => Array
                        (
                            [0] => 10
                            [1] => 20
                            [2] => 50
                            [3] => 100
                        )

                    [degradation] => 1
                    [upsellingDenomination] => 20
                    [collectionOrderDenominations] => 
                    [isExotic] => 
                )

            [2] => Array
                (
                    [baseCurrency] => Array
                        (
                            [id] => 826
                            [description] => Great British Pound
                            [isoAlphaCode] => GBP
                        )

                    [fxCurrency] => Array
                        (
                            [id] => 36
                            [description] => Australian Dollar
                            [isoAlphaCode] => AUD
                        )

                    [buyNote] => 5.95
                    [sellNote] => 1.6201
                    [buyCheque] => 5.95
                    [sellCheque] => 
                    [rank] => LOW
                    [denominations] => Array
                        (
                            [0] => 20
                            [1] => 50
                        )

                    [degradation] => 5
                    [upsellingDenomination] => 20
                    [collectionOrderDenominations] => 
                    [isExotic] => 1
                )

            [3] => Array
                (
                    [baseCurrency] => Array
                        (
                            [id] => 826
                            [description] => Great British Pound
                            [isoAlphaCode] => GBP
                        )

                    [fxCurrency] => Array
                        (
                            [id] => 48
                            [description] => Bahraini Dinar
                            [isoAlphaCode] => BHD
                        )

                    [buyNote] => 8.7
                    [sellNote] => 0.4456
                    [buyCheque] => 
                    [sellCheque] => 
                    [rank] => LOW
                    [denominations] => Array
                        (
                            [0] => 10
                            [1] => 20
                        )

                    [degradation] => 1
                    [upsellingDenomination] => 1
                    [collectionOrderDenominations] => 
                    [isExotic] => 1
                )

            [4] => Array
                (
                    [baseCurrency] => Array
                        (
                            [id] => 826
                            [description] => Great British Pound
                            [isoAlphaCode] => GBP
                        )

                    [fxCurrency] => Array
                        (
                            [id] => 52
                            [description] => Barbados Dollar
                            [isoAlphaCode] => BBD
                        )

                    [buyNote] => 9.7
                    [sellNote] => 2.324
                    [buyCheque] => 
                    [sellCheque] => 
                    [rank] => LOW
                    [denominations] => Array
                        (
                            [0] => 20
                            [1] => 50
                            [2] => 100
                        )

                    [degradation] => 2
                    [upsellingDenomination] => 2
                    [collectionOrderDenominations] => 
                    [isExotic] => 1
                )

        )

    [valid] => 1
    [errorMessage] => 
    [errorCauses] => 
)
  • 写回答

1条回答 默认 最新

  • dtwr2012 2017-04-06 10:30
    关注
    $arr = json_decode('{"wrappedObject":[{"baseCurrency":{"id":826,"description":"Great British Pound","isoAlphaCode":"GBP"}...', true);
    
    echo $arr["wrappedObject"][0]["baseCurrency"]["isoAlphaCode"]; // outputs "GBP"
    

    Use this bit of code.

    It uses the json_decode method that you have been using already. Just make sure you set the second parameter to true.
    What that will do is store the JSON as an array rather than an object.

    Update

    Following @user2401723 comment below.

    I don't fully understand what you are storing in the JSON, but it seems that you take the user's base currency (I assume the currency of their country) and that is stored as "baseCurrency" and repeated in each property in the object.

    Consider this line of code.

    echo $arr["wrappedObject"][0]["baseCurrency"]["isoAlphaCode"]; // outputs "GBP"
    echo $arr["wrappedObject"][0]["sellNote"]; // outputs 1.1495
    

    This outputs the user's base currency.

    Since [0] is the Euro (obtained by writing $arr["wrappedObject"][0]["fxCurrency"]["isoAlphaCode"]), then I assume the sellNote is the exchange rate between baseCurrency and fxCurrency.

    I don't know how you want to use the data but I will write a simple for loop so you can see all the isoAlphaCodes and the sellNotes.

    // this first line will display your base currency
    echo $arr["wrappedObject"][0]["baseCurrency"]["isoAlphaCode"];
    // this for loop will display every currency code and their exchange rate (if that's what sellNote is)
    for ($i = 0; $i < count($arr["wrappedObject"]); $i++)
    {
        echo $arr["wrappedObject"][$i]["fxCurrency"]["isoAlphaCode"];
        echo $arr["wrappedObject"][$i]["sellNote"];
    }
    

    MySQLi

    Regards to inserting it into the MySQL, I don't know whether you are using PDO or MySQLi so I will give you an example of MySQLi.

    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "myDBname";
    
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    
    for ($i = 0; $i < count($arr["wrappedObject"]); $i++)
    {
        $isoAlphaCode = $arr["wrappedObject"][$i]["fxCurrency"]["isoAlphaCode"];
        $sellNote     = $arr["wrappedObject"][$i]["sellNote"];
    
        $query = $mysqli->prepare("INSERT INTO myTable(isoAlphaCode, sellNote) VALUES(?, ?)");  
        $query->bind_param("sd", $isoAlphaCode, $sellNote);
        $query->execute();
    }
    $query->close();
    $mysqli->close();
    

    Line by line:

    1. $dbhost - The host of your database.
    2. $dbuser - The username of your database.
    3. $dbpass - The password for your database.
    4. $dbname - The name of your database.
    5. Newline.
    6. $mysqli = new mysqli(...) - Creating the MySQLi object. See the documentation. This will store our connection to the database.
    7. Newline.
    8. for ($i = 0; $i < count($arr["wrappedObject"]); $i++) - Since all of the currencies are stored under $arr["wrappedObject"] we access this index and then count to determine the length.
    9. Opening code block.
    10. $isoAlphaCode - Store the currency isoAlphaCode in this variable. We are referencing the $i index in the array.
    11. $sellNote - The float exchange rate .
    12. Newline.
    13. $mysqli->prepare("... VALUES(?, ?") - The prepared statement.
      It is extremely important to use prepared statements since it will prevent SQL injections.
      ? Tells the query that we are going to insert data here.
    14. bind_param("sd", $isoAlphaCode, $sellNote) - Add our parameters to the query.
      "sd" - tells the query that our first ? is a string and the second is a double. Then we simply list our parameters after this in the order that relates to the ?.
    15. $query->execute() - execute the query.
    16. Closing code block.
    17. $query->close() - Close the query
    18. $mysqli->close() - Close the connection (only once finished with it).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿