2015-09-06 94 views
1

我使用外部.csv文件來推斷php中的一些值並進行計算。我的.csv以下列方式結構化(這是一個非常簡單的例子,我的CSV通常有50/60行每個人用不同的名稱):csv to php - 根據第一列的值選擇特定的行

銷售,10,24,56
收入,14, 71,105

我想使用PHP指令來選擇只有名爲「收入」或只有名爲「銷售」的行。到目前爲止,我已經使用了「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>'; 

如果「銷售」和「收入」有不同的位置,我無法調用正確的值。

UPDATE:

這是的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

SO,得到行的第一個值,並檢查它是否是'sales'。 –

+0

你的代碼在哪裏? – aldrin27

+0

@u_mulder嗨,你建議申請一個if?如何在內部構建for循環?感謝您的回覆 – mauro269

回答

0

首先使用preg_grep & filestr_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 
} 
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>'; 
0

試試這個

$sales = []; 
$revenues = []; 
foreach($csv as $key => $val) 
{ 
    $sales[] = $val['Sales']; 
    $revenues[] $val['Revenue']; 
} 
print_r($sales); 
print_r($revenues); 

如果你想獲得的所有數據的:

$data = []; 
foreach($csv as $key => $val) 
{ 
$data[] = ['Sales' => $val['Sales'], 'Revenue' => $val['Revenue'], ......]; // so on and so forth. 
} 
print_r($data);