drazvzi741287 2015-09-06 08:37
浏览 41

csv to php - 根据第一列的值选择特定行

I'm using an external .csv file to extrapolate some values in php and do calculations. My .csv is structured in the following way (this is a very simple example, my csv usually has 50/60 rows everyone with a different name):

Sales,10,24,56
Revenues,14,71,105

I would like to use a php instruction to select only the row named "Revenues" or only that named "Sales". Until now I've used a "for" loop but that is based only on a i++ variable.

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

If "sales" and "revenues" have a different position I'm not able to call the right values.

UPDATE:

Here it is the 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
)
  • 写回答

3条回答 默认 最新

  • doupiao1893 2015-09-06 09:04
    关注

    First use preg_grep & file with str_getcsv to solve your problem.

    First $file = file('csv/sample.csv');

    then $revenues = preg_grep ('/^Revenues/i', $file);

    then

    for ($revenues as $revenue)
    {
        $revenue = str_getcsv($revenue);
        echo $revenue[0] . $revenue[1] . $revenue[2] . $revenue[3];//Revenue key 0,1,2,3
    }
    

    then $sales = preg_grep ('/^Sales/i', $file);

    then

    for ($sales as $sale)
    {
        $sale = str_getcsv($sale);
        echo $sale[0] . $sale[1] . $sale[2] . $sale[3];//Sale key 0,1,2,3
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大