【问题标题】:how to convert array to mysql in php如何在php中将数组转换为mysql
【发布时间】:2017-04-06 09:43:15
【问题描述】:

有谁知道我如何从以下数组中提取货币 (isoAlphaCode) 和 sellNote 并将其添加到 MySQL 数据库中?

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] => 
)

【问题讨论】:

  • “后面的数组”从何而来?
  • 来自 json 文件
  • 这是一个 PHP 数组的print_r... JSON 是从哪里来的?

标签: php mysql arrays


【解决方案1】:
$arr = json_decode('{"wrappedObject":[{"baseCurrency":{"id":826,"description":"Great British Pound","isoAlphaCode":"GBP"}...', true);

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

使用这段代码。

它使用您已经使用的json_decode 方法。只需确保将第二个参数设置为 true
这样做是将 JSON 存储为 array rather than an object

更新

关注下方@user2401723 评论。

我不完全理解您在 JSON 中存储的内容,但您似乎采用了用户的基础货币(我假设他们的国家/地区的货币)并且存储为 "baseCurrency" 并在每个属性中重复对象。

考虑这行代码。

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

这会输出用户的基础货币。

由于[0] 是欧元(通过写$arr["wrappedObject"][0]["fxCurrency"]["isoAlphaCode"] 获得),那么我假设sellNotebaseCurrencyfxCurrency 之间的汇率。

我不知道你想如何使用这些数据,但我会写一个简单的 for 循环,这样你就可以看到所有的 isoAlphaCodes 和 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

关于插入到MySQL中,不知道你用的是PDO还是MySQLi,我举个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();

逐行:

  1. $dbhost - 数据库的主机。
  2. $dbuser - 您的数据库的用户名。
  3. $dbpass - 您的数据库的密码。
  4. $dbname - 您的数据库的名称。
  5. 换行。
  6. $mysqli = new mysqli(...) - 创建 MySQLi 对象。请参阅documentation。这将存储我们与数据库的连接。
  7. 换行。
  8. for ($i = 0; $i &lt; count($arr["wrappedObject"]); $i++) - 由于所有货币都存储在 $arr["wrappedObject"] 下,我们访问此索引,然后访问 count 以确定长度。
  9. 打开代码块。
  10. $isoAlphaCode - 将货币 isoAlphaCode 存储在此变量中。我们正在引用数组中的$i 索引。
  11. $sellNote - float 汇率。
  12. 换行。
  13. $mysqli-&gt;prepare("... VALUES(?, ?") - prepared statement.
    使用准备好的语句非常重要,因为它会prevent SQL injections
    ? 告诉查询我们将在此处插入数据。
  14. bind_param("sd", $isoAlphaCode, $sellNote) - 将我们的参数添加到查询中。
    "sd" - 告诉查询我们的第一个 ?string,第二个是 double。然后我们只需按照与? 相关的顺序在此之后列出我们的参数。
  15. $query-&gt;execute() - 执行查询。
  16. 关闭代码块。
  17. $query-&gt;close() - 关闭查询
  18. $mysqli-&gt;close() - 关闭连接(仅在完成后)。

【讨论】:

  • 谢谢,但是您如何获得每种货币的 isoAlphaCode 和 sellNote?
猜你喜欢
  • 2014-04-30
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 2011-09-28
相关资源
最近更新 更多