簡體   English   中英

JSON 解碼保存數組

[英]JSON Decode save array

我可以使用以下 PHP 代碼接收以下 JSON 數據:

$json = file_get_contents('https://xxxx');
$data = json_decode($json,true);
$forex = $data['items'];
echo "<pre>";
print_r($forex);
exit;

這給了我以下 JSON 數據:

Array
  (
   [0] => Array
    (
        [new] => 
        [data] => Array
            (
                [direction] => 1
                [pip] => 0
                [exchange] => FOREX
                [symbol] => USDCHF
                [interval] => 15
                [pattern] => Resistance
                [complete] => 
                [identified] => 2020-02-26T14:45:00.000Z
                [age] => 0
                [length] => 259
                [found] => 2020-02-26T14:45:56.381Z
                [result_type] => KeyLevel
                [result_uid] => 642525551
                [prediction_price_from] => 0
                [prediction_price_to] => 0
                [group_name] => FX Majors
                [symbol_name] => USDCHF
                [symbol_id] => 0
                [analysis_text] => Approaching Resistance level of 0.9800 identified at 2/26 14:45. This pattern is still in the process of forming. Possible bullish price movement towards the resistance 0.9800 within the next 15 hours.
                [expires_at] => 2020-02-27T05:48:36.701Z
            )

        [links] => Array
            (
                [0] => Array
                    (
                        [rel] => detail
                        [href] => https://xxxx
                    )

                [1] => Array
                    (
                        [rel] => analysis
                        [href] => https://xxxx
                    )

                [2] => Array
                    (
                        [rel] => chart-xs
                        [href] => https://xxxx
                    )

                [3] => Array
                    (
                        [rel] => chart-sm
                        [href] => https://xxxx
                    )

                [4] => Array
                    (
                        [rel] => chart-md
                        [href] => https://xxxx
                    )

                [5] => Array
                    (
                        [rel] => chart-lg
                        [href] => https://xxxx
                    )

                [6] => Array
                    (
                        [rel] => icon-arrow
                        [href] => https://xxxx
                    )

            )

    )

我想要做的是將信息保存到我的 MySQL 數據庫中,但是即使通過測試回顯數據,我也無法顯示數據:

foreach($forex as $info) { 
echo $info->symbol . '<br>';
}

任何有可能解決方案的人都可以保存數據,甚至讓我能夠顯示每個數組的數據(例如:符號、模式、鏈接圖表-md)。

在注釋代碼下方:

//Save json text into variable $json
$json = file_get_contents('https://xxxx');

//Convert json into PHP array
$data = json_decode($json,true);

//Set $forex as $data['items'];
$forex = $data['items'];

//Print
echo "<pre>";
print_r($forex);
exit;

$forex是一個 PHP 數組變量(不是 JSON 字符串)。

您可以使用 json 字符串變量$json而不是 PHP 數組$forex將數據保存到 MySQL VARCHAR[Length]字段中。

當您需要從 DB 獲取數據並顯示它時,您可以從 MySQL 獲取 JSON 數據並使用 PHP json_decode($json,true)將其從 JSON 轉換為數組。

如果要在 PHP 數組中使用值,請使用 $info["symbol"] 而不是$info->symbol

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM