在数组php中遍历数组以获取值

时间:2018-11-11 02:32:47

标签: php arrays

我在数组中有一个数组,我想遍历该数组以获取值,以便将它们存储在数据库中。在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 [candles] 0、1、2、3-500等,我想将这些值存储到变量中,这样就可以对数据库进行检查以检查是否存在,或者将这些值用于每个特定的[candles]数组值的数据库插入。 timeohlc是数据的重要部分。

1 个答案:

答案 0 :(得分:0)

您很近。但是,您引用的数组索引不正确。

让我们将其分为两部分。首先,让我们将数据压缩到一个不错的数组中,以后可以用于查询。

步骤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 Parameterized Queries

他们也有一个PDO

PDO Parameterized Queries

为这些添加书签并经常引用它们。他们还将向您展示如何正确设置数据库连接。

希望这会有所帮助。