我正在使用外部.csv文件来推断php中的一些值并进行计算。我的.csv的结构方式如下(这是一个非常简单的例子,我的csv通常有50/60行,每个人都有不同的名字):
销售额,10,24,56年收入,14,71,105
我想使用php指令只选择名为“Revenues”的行或仅选择名为“Sales”的行。到目前为止,我已经使用了“for”循环,但这仅基于i ++变量。
$csv = str_getcsv(file_get_contents('csv/sample.csv'));
for ($i = 12, $k=$i+11; $i < $k; $i++) {
echo '<td>';
echo number_format($csv[$i],0,'.',',');
echo '</td>';
}
echo '</tr>';
如果“销售”和“收入”的位置不同,我就无法调用正确的值。
更新:
这是print_r($ csv)
Array
(
[0] => USD in millions.
[1] => 2005-09
[2] => 2006-09
[3] => 2007-09
[4] => 2008-09
[5] => 2009-09
[6] => 2010-09
[7] => 2011-09
[8] => 2012-09
[9] => 2013-09
[10] => 2014-09
[11] => TTM
Revenue
[12] => 13931
[13] => 19315
[14] => 24006
[15] => 32479
[16] => 42905
[17] => 65225
[18] => 108249
[19] => 156508
[20] => 170910
[21] => 182795
[22] => 199800
Cost of revenue
[23] => 9888
[24] => 13717
[25] => 15852
[26] => 21334
[27] => 25683
[28] => 39541
[29] => 64431
[30] => 87846
[31] => 106606
[32] => 112258
[33] => 121368
Gross profit
[34] => 4043
[35] => 5598
[36] => 8154
[37] => 11145
[38] => 17222
[39] => 25684
[40] => 43818
[41] => 68662
[42] => 64304
[43] => 70537
[44] => 78432
Operating expenses
Research and development
[45] => 534
[46] => 712
[47] => 782
[48] => 1109
[49] => 1333
[50] => 1782
[51] => 2429
[52] => 3381
[53] => 4475
[54] => 6041
[55] => 6606
Sales
[56] => General and administrative
[57] => 1859
[58] => 2433
[59] => 2963
[60] => 3761
[61] => 4149
[62] => 5517
[63] => 7599
[64] => 10040
[65] => 10830
[66] => 11993
[67] => 12540
Total operating expenses
[68] => 2393
[69] => 3145
[70] => 3745
[71] => 4870
[72] => 5482
[73] => 7299
[74] => 10028
[75] => 13421
[76] => 15305
[77] => 18034
[78] => 19146
Operating income
[79] => 1650
[80] => 2453
[81] => 4409
[82] => 6275
[83] => 11740
[84] => 18385
[85] => 33790
[86] => 55241
[87] => 48999
[88] => 52503
[89] => 59286
Interest Expense
[90] =>
[91] =>
[92] =>
[93] =>
[94] =>
[95] =>
[96] =>
[97] =>
[98] => 136
[99] => 384
[100] => 431
Other income (expense)
[101] => 165
[102] => 365
[103] => 599
[104] => 620
[105] => 326
[106] => 155
[107] => 415
[108] => 522
[109] => 1292
[110] => 1364
[111] => 1335
Income before taxes
[112] => 1815
[113] => 2818
[114] => 5008
[115] => 6895
[116] => 12066
[117] => 18540
[118] => 34205
[119] => 55763
[120] => 50155
[121] => 53483
[122] => 60190
Provision for income taxes
[123] => 480
[124] => 829
[125] => 1512
[126] => 2061
[127] => 3831
[128] => 4527
[129] => 8283
[130] => 14030
[131] => 13118
[132] => 13973
[133] => 15728
Net income from continuing operations
[134] => 1335
[135] => 1989
[136] => 3496
[137] => 4834
[138] => 8235
[139] => 14013
[140] => 25922
[141] => 41733
[142] => 37037
[143] => 39510
[144] => 44462
Net income
[145] => 1335
[146] => 1989
[147] => 3496
[148] => 4834
[149] => 8235
[150] => 14013
[151] => 25922
[152] => 41733
[153] => 37037
[154] => 39510
[155] => 44462
Net income available to common shareholders
[156] => 1335
[157] => 1989
[158] => 3496
[159] => 4834
[160] => 8235
[161] => 14013
[162] => 25922
[163] => 41733
[164] => 37037
[165] => 39510
[166] => 44462
Earnings per share
Basic
[167] => 0.24
[168] => 0.34
[169] => 0.58
[170] => 0.78
[171] => 1.32
[172] => 2.20
[173] => 4.01
[174] => 6.38
[175] => 5.72
[176] => 6.49
[177] => 7.44
Diluted
[178] => 0.22
[179] => 0.32
[180] => 0.56
[181] => 0.77
[182] => 1.30
[183] => 2.16
[184] => 3.95
[185] => 6.31
[186] => 5.68
[187] => 6.45
[188] => 7.39
Weighted average shares outstanding
Basic
[189] => 5659
[190] => 5908
[191] => 6052
[192] => 6171
[193] => 6251
[194] => 6366
[195] => 6470
[196] => 6544
[197] => 6477
[198] => 6086
[199] => 5978
Diluted
[200] => 5997
[201] => 6143
[202] => 6225
[203] => 6315
[204] => 6349
[205] => 6473
[206] => 6557
[207] => 6617
[208] => 6522
[209] => 6123
[210] => 6016
EBITDA
[211] => 1829
[212] => 2678
[213] => 4726
[214] => 6748
[215] => 12474
[216] => 19412
[217] => 35604
[218] => 58518
[219] => 57048
[220] => 61813
[221] => 68998
)
答案 0 :(得分:0)
首先使用preg_grep
&amp; file
与str_getcsv
一起解决您的问题。
首先$file = file('csv/sample.csv');
然后$revenues = preg_grep ('/^Revenues/i', $file);
然后
for ($revenues as $revenue)
{
$revenue = str_getcsv($revenue);
echo $revenue[0] . $revenue[1] . $revenue[2] . $revenue[3];//Revenue key 0,1,2,3
}
然后$sales = preg_grep ('/^Sales/i', $file);
然后
for ($sales as $sale)
{
$sale = str_getcsv($sale);
echo $sale[0] . $sale[1] . $sale[2] . $sale[3];//Sale key 0,1,2,3
}
答案 1 :(得分:0)
您可以将csv读入命名的键数组,然后按名称引用相关的行条目。
$csv = array();
$file = new SplFileObject( "csv/sample.csv" );
$file->setFlags( SplFileObject::READ_CSV );
foreach( $file as $index => $row ) {
$csv[ strtolower( $row[0] )]=array_splice( $row, 1, count( $row ) );
}
echo '<pre>',print_r($csv['sales'],true),'</pre>';
答案 2 :(得分:0)
试试这个
.search-icon {
font-size: 1.875em;
color: #1CB6B5;
position: absolute;
right: 0px;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
.search-icon-open {
z-index: 1000;
color: #FFF;
right: 400px;
}
如果您希望获得所有数据:
$sales = [];
$revenues = [];
foreach($csv as $key => $val)
{
$sales[] = $val['Sales'];
$revenues[] $val['Revenue'];
}
print_r($sales);
print_r($revenues);