Loop through an array within an array php to get values











up vote
-1
down vote

favorite












I have an array inside an array and I want to loop through the array to get the values so I can store them in a database. What would be the best way to approach this in PHP?



The array:



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
)

)
)
)


I wish to get the values like so:



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;
}


This array can contain say 500 [candles] 0,1,2,3 - 500 etc I want to store the values into variables so I can do a check on my database to check if exists or then use the values for a database insert for each specific [candles] array values. time, o, h, l and c being the important parts of the data.










share|improve this question
























  • Excellent example with code. But what is the question?
    – ivanivan
    Nov 11 at 2:50










  • What flavor of SQL are you using? PDO or MySQLi?
    – Joseph_J
    Nov 11 at 4:08

















up vote
-1
down vote

favorite












I have an array inside an array and I want to loop through the array to get the values so I can store them in a database. What would be the best way to approach this in PHP?



The array:



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
)

)
)
)


I wish to get the values like so:



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;
}


This array can contain say 500 [candles] 0,1,2,3 - 500 etc I want to store the values into variables so I can do a check on my database to check if exists or then use the values for a database insert for each specific [candles] array values. time, o, h, l and c being the important parts of the data.










share|improve this question
























  • Excellent example with code. But what is the question?
    – ivanivan
    Nov 11 at 2:50










  • What flavor of SQL are you using? PDO or MySQLi?
    – Joseph_J
    Nov 11 at 4:08















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have an array inside an array and I want to loop through the array to get the values so I can store them in a database. What would be the best way to approach this in PHP?



The array:



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
)

)
)
)


I wish to get the values like so:



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;
}


This array can contain say 500 [candles] 0,1,2,3 - 500 etc I want to store the values into variables so I can do a check on my database to check if exists or then use the values for a database insert for each specific [candles] array values. time, o, h, l and c being the important parts of the data.










share|improve this question















I have an array inside an array and I want to loop through the array to get the values so I can store them in a database. What would be the best way to approach this in PHP?



The array:



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
)

)
)
)


I wish to get the values like so:



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;
}


This array can contain say 500 [candles] 0,1,2,3 - 500 etc I want to store the values into variables so I can do a check on my database to check if exists or then use the values for a database insert for each specific [candles] array values. time, o, h, l and c being the important parts of the data.







php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 2:39









Nick

20.4k51434




20.4k51434










asked Nov 11 at 2:32









xop32

481310




481310












  • Excellent example with code. But what is the question?
    – ivanivan
    Nov 11 at 2:50










  • What flavor of SQL are you using? PDO or MySQLi?
    – Joseph_J
    Nov 11 at 4:08




















  • Excellent example with code. But what is the question?
    – ivanivan
    Nov 11 at 2:50










  • What flavor of SQL are you using? PDO or MySQLi?
    – Joseph_J
    Nov 11 at 4:08


















Excellent example with code. But what is the question?
– ivanivan
Nov 11 at 2:50




Excellent example with code. But what is the question?
– ivanivan
Nov 11 at 2:50












What flavor of SQL are you using? PDO or MySQLi?
– Joseph_J
Nov 11 at 4:08






What flavor of SQL are you using? PDO or MySQLi?
– Joseph_J
Nov 11 at 4:08














1 Answer
1






active

oldest

votes

















up vote
0
down vote













You are close. However, you are referencing some of your array indexes incorrectly.



Lets break this down into two parts. First lets just condense your data into a nice array that you can later use for your queries.



Step 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>';


Now you have an array containing just the information you want for each item.



Step 2:



You will need to be able have a valid connection to a database and know your table and column names. But here is an example of how you would use the newly created array to perform your queries.



//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();


If you are new to parameterized queries you should read this link.



MySQLi Parameterized Queries



They also have one for PDO



PDO Parameterized Queries



Bookmark these and refer to them often. They will also show you how to set up your DB connections properly.



Hope this helps.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245351%2floop-through-an-array-within-an-array-php-to-get-values%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You are close. However, you are referencing some of your array indexes incorrectly.



    Lets break this down into two parts. First lets just condense your data into a nice array that you can later use for your queries.



    Step 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>';


    Now you have an array containing just the information you want for each item.



    Step 2:



    You will need to be able have a valid connection to a database and know your table and column names. But here is an example of how you would use the newly created array to perform your queries.



    //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();


    If you are new to parameterized queries you should read this link.



    MySQLi Parameterized Queries



    They also have one for PDO



    PDO Parameterized Queries



    Bookmark these and refer to them often. They will also show you how to set up your DB connections properly.



    Hope this helps.






    share|improve this answer

























      up vote
      0
      down vote













      You are close. However, you are referencing some of your array indexes incorrectly.



      Lets break this down into two parts. First lets just condense your data into a nice array that you can later use for your queries.



      Step 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>';


      Now you have an array containing just the information you want for each item.



      Step 2:



      You will need to be able have a valid connection to a database and know your table and column names. But here is an example of how you would use the newly created array to perform your queries.



      //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();


      If you are new to parameterized queries you should read this link.



      MySQLi Parameterized Queries



      They also have one for PDO



      PDO Parameterized Queries



      Bookmark these and refer to them often. They will also show you how to set up your DB connections properly.



      Hope this helps.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You are close. However, you are referencing some of your array indexes incorrectly.



        Lets break this down into two parts. First lets just condense your data into a nice array that you can later use for your queries.



        Step 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>';


        Now you have an array containing just the information you want for each item.



        Step 2:



        You will need to be able have a valid connection to a database and know your table and column names. But here is an example of how you would use the newly created array to perform your queries.



        //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();


        If you are new to parameterized queries you should read this link.



        MySQLi Parameterized Queries



        They also have one for PDO



        PDO Parameterized Queries



        Bookmark these and refer to them often. They will also show you how to set up your DB connections properly.



        Hope this helps.






        share|improve this answer












        You are close. However, you are referencing some of your array indexes incorrectly.



        Lets break this down into two parts. First lets just condense your data into a nice array that you can later use for your queries.



        Step 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>';


        Now you have an array containing just the information you want for each item.



        Step 2:



        You will need to be able have a valid connection to a database and know your table and column names. But here is an example of how you would use the newly created array to perform your queries.



        //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();


        If you are new to parameterized queries you should read this link.



        MySQLi Parameterized Queries



        They also have one for PDO



        PDO Parameterized Queries



        Bookmark these and refer to them often. They will also show you how to set up your DB connections properly.



        Hope this helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 4:31









        Joseph_J

        2,7401618




        2,7401618






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245351%2floop-through-an-array-within-an-array-php-to-get-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."