我有一些问题,我需要指导。我试图解析3个csv文件并将它们放在foreach循环中,但它可怕的错误。字符串$ stockClose不会改变它在整个循环中保持不变,$ nas_stock每20个循环更改一次,并且假设每个循环都会改变,唯一似乎正常工作的是$ sp500_stock,我不知道为什么
$currentMonth = date('n');
$currentMonth = $currentMonth - 1;
$prevcurrentMonth = date('n');
$prevcurrentMonth = $prevcurrentMonth - 2;
$currentDay = date('j');
$lastDay = $currentDay - 6;
$currentYear = date('Y');
$filedow = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=%5EDJI&a=$prevcurrentMonth&b=$lastDay&c=$currentYear&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&ignore=.csv");
$filenas = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=%5EIXIC&a=$prevcurrentMonth&b=$lastDay&c=$currentYear&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&ignore=.csv");
$filesp500 = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=%5EGSPC&a=$prevcurrentMonth&b=$lastDay&c=$currentYear&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&ignore=.csv");
$stockcontent = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '', $filedow);
$stockcontent = trim($stockcontent);
$stockcontentex = str_getcsv($stockcontent, "\n");
$stocknas = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '', $filenas);
$stocknas = trim($stocknas);
$stocknasex = str_getcsv($stocknas, "\n");
$stocksp500 = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '', $filesp500);
$stocksp500 = trim($stocksp500);
$stocksp500ex = str_getcsv($stocksp500, "\n");
$i = 0;
$j = 0;
$_str = '';
$_str .= "<script type='text/javascript'>
google.load('visualization', '1', {packages: ['annotatedtimeline']});
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Dow Jones');
data.addColumn('number', 'Nasdaq');
data.addColumn('number', 'S&P 500');
data.addRows([";
$tstr = "";
$_str = '';
$_str .= "<script type='text/javascript'>
google.load('visualization', '1', {packages: ['annotatedtimeline']});
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Dow Jones');
data.addColumn('number', 'Nasdaq');
data.addColumn('number', 'S&P 500');
data.addRows([";
$tstr = "";
foreach($stocknasex as $nas){
$nasex = explode(',',$nas);
$nas_stock = $nasex[4];
}
foreach($stocksp500ex as $sp500){
$sp500ex = explode(',',$sp500);
$sp500_stock = $sp500ex[4];
}
foreach($stockcontentex as $stockexplode){
$stockex = explode(',',$stockexplode);
$stockexdate = explode('-', $stockex[0]);
$stockYear = $stockexdate[0];
$stockMonth = $stockexdate[1] - 1 ;
$stockDay = $stockexdate[2];
$stockHigh = $stockex[2];
$stockLow = $stockex[3];
$stockClose = $stockex[4];
}
for($i=0; $i<=30; $i++){
$tstr = '[new Date('.$stockYear.', '.$stockMonth.', '.$stockDay.'), '.$stockClose.', '.$nas_stock.', '.$sp500_stock.'],'. "\n".$tstr;
}
$_str = $_str.$tstr;
$_str .= "]);
var annotatedtimeline = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
annotatedtimeline.draw(data, {
//'allValuesSuffix': '%', // A suffix that is added to all values
'colors': ['#12577F', '#769422', '#999999'], // The colors to be used
'displayAnnotations': true,
'displayExactValues': true, // Do not truncate values (i.e. using K suffix)
'displayRangeSelector' : false, // Do not sow the range selector
'displayZoomButtons': true, // DO not display the zoom buttons
'legendPosition': 'newRow', // Can be sameRow
//'max': 600, // Override the automatic default
//'min': 500, // Override the automatic default
'scaleColumns': [0, 1], // Have two scales, by the first and second lines
'scaleType': 'allmaximized', // See docs...
'thickness': 2, // Make the lines thicker
});
}
google.setOnLoadCallback(drawVisualization);
</script><div style='float:left; border:1px solid #ccc;'>
<div id='chart_div' style='width: 500px; height: 230px;'></div></div>";
return $_str;
break;
}
答案 0 :(得分:1)
你想做什么?你有嵌套循环。整个$sp500
循环位于$nas
循环体内,这意味着$nas
将不会重复,直到$sp500
一直到$stocksp500ex
的第一个值1}}。反过来,$nas
循环完全包含在$stockexplode
循环的正文中,这意味着$stockexplode
在$nas
完全消失之前不会前进通过$stocknasex
。
他们就像一个柜台的数字。最里面的循环是最右边的数字,下一个循环不会点击,直到内部循环。
修改强>: 如果您尝试并行迭代所有三个数组,则应使用一个循环而不是三个嵌套循环。也许是这样的?
$nasex_count = count($stocknasex);
$sp500_count = count($stocksp500ex);
$content_count = count($stockcontentex);
$max = max($nasex_count, $sp500_count, $content_count);
for ($i = 0; $i < $max; ++$i) {
if ($i < $nasex_count) {
... do something with $stocknasex[$i] ...
}
if ($i < $sp500_count) {
... do something with $stocksp500ex[$i] ...
}
if ($i < $content_count) {
... do something with $stockcontentex[$i] ...
}
}
当然,如果您知道所有三个数组的大小相同,则可以跳过max()
和条件。