首页 > 解决方案 > 循环遍历数组php中的数组以获取值

问题描述

我在数组中有一个数组,我想遍历数组以获取值,以便可以将它们存储在数据库中。在 PHP 中解决这个问题的最佳方法是什么?

数组:

Array
(
    [instrument] => AUD_CAD
    [granularity] => H1
    [candles] => Array
    (
        [0] => Array
            (
                [complete] => 1
                [volume] => 942
                [time] => 2018-06-03T21:00:00.000000000Z
                [bid] => Array
                    (
                        [o] => 0.97957
                        [h] => 0.98054
                        [l] => 0.97957
                        [c] => 0.98048
                    )

                [mid] => Array
                    (
                        [o] => 0.98032
                        [h] => 0.98083
                        [l] => 0.98022
                        [c] => 0.98076
                    )

                [ask] => Array
                    (
                        [o] => 0.98107
                        [h] => 0.98133
                        [l] => 0.98050
                        [c] => 0.98105
                    )

            )

        [1] => Array
            (
                [complete] => 1
                [volume] => 888
                [time] => 2018-06-03T22:00:00.000000000Z
                [bid] => Array
                    (
                        [o] => 0.98048
                        [h] => 0.98069
                        [l] => 0.97972
                        [c] => 0.97986
                    )

                [mid] => Array
                    (
                        [o] => 0.98077
                        [h] => 0.98093
                        [l] => 0.97989
                        [c] => 0.97998
                    )

                [ask] => Array
                    (
                        [o] => 0.98106
                        [h] => 0.98124
                        [l] => 0.98000
                        [c] => 0.98011
                    )

            )
    )
)

我希望得到这样的值:

foreach ($get_instruments_candles['candles'] as $candle) {
    // first array part
    $instrument = $candle['instrument'];
    $granularity = $candle['granularity'];

    // one level deeper into the array
    $complete = $candle[0]['complete'];
    $volume = $candle[0]['volume'];

    //another level deeper
    $open = $candle[0]['mid']['o'];
    $high = $candle[0]['mid']['h'];
    $low = $candle[0]['mid']['l'];
    $close = $candle[0]['mid']['c'];

    // check if exists in db
    // do a check here or insert data
    echo 'insert in db  ins= '. $instrument. ' gran='. $granularity .' com= '. $complete .' open =' .$open. ' high = ' . $high . ' low = ' . $low . ' close = ' . $close;
 }

该数组可以包含 500 [蜡烛] 0,1,2,3 - 500 等每个特定的 [candles] 数组值。time, o, h,是数据的重要部分lc

标签: phparrays

解决方案


你很亲密。但是,您错误地引用了一些数组索引。

让我们把它分成两部分。首先让我们将您的数据压缩成一个很好的数组,您可以稍后将其用于查询。

步骤1:

foreach ($get_instruments_candles['candles'] as $candle) {

    //Create an array containing just the information that you want for each item.
    $newArray[] = array(

      'ins'     => $get_instruments_candles['instrument'],
      'gran'    => $get_instruments_candles['granularity'],
      'com'     => $candle['complete'],
      'volume'  => $candle['volume'],
      'open'    => $candle['mid']['o'],
      'high'    => $candle['mid']['h'],
      'low'     => $candle['mid']['l'],
      'close'   => $candle['mid']['c']

    ); 

}

echo '<pre>';
print_r($newArray);
echo '</pre>';

现在您有了一个数组,其中只包含您想要的每个项目的信息。

第2步:

您将需要能够与数据库建立有效连接并知道您的表名和列名。但这里有一个示例,说明如何使用新创建的数组来执行查询。

//Here is an example of an parameterized insert query.
$query = "INSERT INTO YOURTABLE 
(
  ins,
  gran,
  com,
  volume,
  open,
  high,
  low,
  close
) VALUES (?,?,?,?,?,?,?,?)";


//Use a loop and iterate across your items and execute your query for each item.
foreach($newArray as $item){

$stmt = $connection->prepare($query);
$stmt->bind_param('ssssssss', ...$item);
$stmt->execute();    

}

$stmt->close();

如果您不熟悉参数化查询,则应阅读此链接。

MySQLi 参数化查询

他们也有一个用于 PDO

PDO 参数化查询

将这些加入书签并经常参考。他们还将向您展示如何正确设置数据库连接。

希望这可以帮助。


推荐阅读