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.
php arrays
add a comment |
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.
php arrays
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
add a comment |
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.
php arrays
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
php arrays
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 4:31
Joseph_J
2,7401618
2,7401618
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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