问题:我在while循环中有一个foreach循环。对于从数据库中选择的每一行,foreach循环必须完成。但是当我回到$ row时,似乎它立即将所有的行放出来,然后只用一次就转向了foreach。我已经阅读了这些手册,但遗憾的是它并没有帮助我。
代码:
$conn = oci_connect('login', 'pass', '127.0.0.1/xe');
$stid = oci_parse($conn, 'select TOKEN, SECRET from TOKENS');
oci_execute($stid);
$time=20:00
while(($row = oci_fetch_row($stid)) != false) {
$fitbit->setOAuthDetails($row[0], $row[1]);
//retreive steps. (with date and time)
$steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');
$n=0;
foreach ($steps as $value){
$sqlArray[$n]['dateTime']=$value->dateTime;
$sqlArray[$n]['time'] = $time;
$sqlArray[$n]['steps'] = $value->value;
$n++;
}
}
我可能忽视了一些事情,但我希望我的问题能够得到解答。
更新的代码:
$time="12:00";
$n=0;
$x=0;
while(($row = oci_fetch_row($stid)) != false) {
$sqlArray[$n]['token'] = $row[0];
$sqlArray[$n]['secret'] = $row[1];
echo 'hoi';
$fitbit->setOAuthDetails($sqlArray[$n]['token'], $sqlArray[$n]['secret']);
//retreive steps. (with date and time)
$steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');
foreach ($steps as $value){
$sqlArray[$n][$x]['dateTime']=$value->dateTime;
$sqlArray[$n][$x]['time'] = $time;
$sqlArray[$n][$x]['steps'] = $value->value;
$x++;
}
//retreive calories and add to sqlArray where date matches. (used for testing now, when acces to intraday api the outcommented code below will be used.
$calories = $fitbit->getTimeSeries('caloriesOut', '2015-06-01', '2015-07-01');
$x=0;
foreach ($calories as $value){
$checkdate=$value->dateTime;
if ($sqlArray[$n][$x]['dateTime'] == $checkdate){
$sqlArray[$n][$x]['calories'] = $value->value;
}
$x++;
}
$n++;
};
现在我的输出如下:
Array
(
[0] => Array
(
[token] => token
[secret] => secret
[0] => Array
(
[dateTime] => 2015-06-01
[time] => 12:00
[steps] => 8046
[calories] => 2785
)
对数据库中的标记进行了很好的迭代。虽然,在第一次迭代后,卡路里被丢弃:
[1] => Array
(
[token] => token
[secret] => secret
[31] => Array
(
[dateTime] => 2015-06-01
[time] => 12:00
[steps] => 8046
)
我想我再次做错了...我认为这与$ x变量有关,但我不确定。
是的,对我来说!我放置$ x = 0;现在在while循环中它运行良好! 谢谢您的帮助!答案 0 :(得分:1)
请在下面填写: -
$conn = oci_connect('login', 'pass', '127.0.0.1/xe');
$stid = oci_parse($conn, 'select TOKEN, SECRET from TOKENS');
oci_execute($stid);
$time='20:00';
$new_array = array(); // create a new array
$i = 0;
while(($row = oci_fetch_row($stid)) != false) {
$new_array[$i][] = $row[0]; // assign values to that new array
$new_array[$i][] = $row[1]; // assign values to that new array
$i++;
}
$sqlArray = array(); // create another new array
$n=0;
foreach($new_array as $array){
$fitbit->setOAuthDetails($array[0], $array[1]); // iterate on first array values
//retreive steps. (with date and time)
$steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');
$sqlArray[$n]['dateTime']=$value->dateTime; // assign value to new array
$sqlArray[$n]['time'] = $time; // assign value to new array
$sqlArray[$n]['steps'] = $value->value; // assign value to new array
$n++;
}
现在你想在$sqlArray
上做什么,这是多维数组,你可以通过echo "<pre/>";print_r($sqlArray);
检查它的结构
答案 1 :(得分:0)
试试这个:
$n=0; // Initiate $n
while(($row = oci_fetch_row($stid)) != false) {
$fitbit->setOAuthDetails($row[0], $row[1]);
$steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');
// Dont initiate $n here
foreach ($steps as $value){
$sqlArray[$n]['dateTime']=$value->dateTime;
$sqlArray[$n]['time'] = $time;
$sqlArray[$n]['steps'] = $value->value;
// Dont increment $n here
}
$n++; // Increment $n 'outside' your foreach
}