2012-01-25 67 views
0

我有一個CSV文件,下面的代碼用於將CSV內容放入多維數組中。從CSV中輸出所有多維數組,數組數據 - PHP

<?php 

$val = $_GET['articleid']; 
//URL Pass STring 

$arrCSV = array(); 
// Open the CSV 
if (($handle = fopen("../articles/artlist.csv", "r")) !==FALSE) {       
// Set the parent array key to 0 
$key = 0; 
// While there is data available loop through unlimited times (0) using separator (,) 
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) { 
    // Count the total keys in each row 
    $c = count($data); 
    //Populate the array 
    for ($x=0;$x<$c;$x++) { 
    $arrCSV[$key][$x] = $data[$x]; 
    } 
    $key++; 
} // end while 
// Close the CSV file 
fclose($handle); 
} // end if 

?> 

我的第一部分是輸出整個數組。

我覺得下面這段代碼是錯誤的:(

<?php  
     for ($row = 1; $row <$arrCSV; $row++) 
      { 
     echo ''.$arrCSV[$row]['6'].''; 
      } 
    ?> 

然後我會把它想只列輸出一個[*] [6]。

我想我應該到這裏來的拳頭之前我嘗試休息。

任何指導,將幫助。

我努力學習,到目前爲止,我還這麼遠了。

謝謝

+0

確定。我需要這部分.. <?php \t \t print_r($ arrCSV); \t?> – Arthor

+0

好的..我正在看... – Arthor

+0

你好,請任何人都可以幫助代碼。 – Arthor

回答

1

這難道不是工作?:

<?php 
$val = $_GET['articleid']; 
//URL Pass STring 

$arrCSV = array(); 
// Open the CSV 
if (($handle = fopen("../articles/artlist.csv", "r")) !==FALSE) {       

// While there is data available loop through unlimited times (0) using separator (,) 
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) { 
    $arrCSV[] = $data; 
} // end while 
// Close the CSV file 
fclose($handle); 
} // end if 

?> 
+0

是的,但...我需要把內容?沒有??? – Arthor

+0

會是? echo $ data ??? – Arthor

+0

This works .. <?php print_r($ arrCSV); ?>我怎麼只能把一列。 – Arthor

1

我用這個功能來CSV轉換爲數組

function csv2array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") 
{ 
    $array = array(); 
    $size = strlen($string); 
    $columnIndex = 0; 
    $rowIndex = 0; 
    $fieldValue = ""; 
    $isEnclosured = False; 

    for($i=0; $i<$size;$i++) 
    { 
     $char = $string{$i}; 
     $addChar = ""; 

     if($isEnclosured) 
     { 
      if($char == $enclosureChar) 
      { 
       if($i+1<$size && $string{$i+1} == $enclosureChar) 
       { 
        $addChar = $char; 
        $i++; 
       } 
       else 
       { 
        $isEnclosured = false; 
       } 
      } 
      else 
      { 
       $addChar=$char; 
      } 
     } 
     else 
     { 
      if($char==$enclosureChar) 
      { 
       $isEnclosured = true; 
      } 
      else 
      { 
       if($char==$separatorChar) 
       { 
        $array[$rowIndex][$columnIndex] = $fieldValue; 
        $fieldValue = ""; 
        $columnIndex++; 
       } 
       elseif($char==$newlineChar) 
       { 
        $array[$rowIndex][$columnIndex] = $fieldValue; 
        $fieldValue=""; 
        $columnIndex=0; 
        $rowIndex++; 
       } 
       else 
       { 
        $addChar=$char; 
       } 
      } 
     } 

     if($addChar != "") 
     { 
      $fieldValue.=$addChar; 
     } 
    } 

    if($fieldValue) 
    { 
     $array[$rowIndex][$columnIndex] = $fieldValue; 
    } 

    return $array; 
} 

您可以簡單地使用這樣的:

<?php 

$handle = fopen("../articles/artlist.csv", "r"); 
$arr = csv2array($handle); 

print_r($arr); 

?> 
+0

我的代碼有效,我只需要這部分工作。 '\t $ searchchar =「cat」; \t \t \t \t爲($行= 1; $行<的sizeof($ arrCSV); $行++)。 \t \t \t { \t \t \t {\t \t \t \t回波 '' $ arrCSV [$行] [ '6'] ''; \t \t \t} \t \t \t} \t?>' – Arthor

+0

我可以將它顯示在列上,但我只想顯示基於另一列列的行。例如紅。列中包含23個RED,這應該是23行。 – Arthor

0

這是解決方案

要訪問CSV:

<?php 

$val = $_GET['articleid']; 
//URL Pass STring 

$arrCSV = array(); 
// Open the CSV 
if (($handle = fopen("../articles/artlist.csv", "r")) !==FALSE) {       
// Set the parent array key to 0 
$key = 0; 
// While there is data available loop through unlimited times (0) using separator (,) 
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) { 
    // Count the total keys in each row 
    $c = count($data); 
    //Populate the array 
    for ($x=0;$x<$c;$x++) { 
    $arrCSV[$key][$x] = $data[$x]; 
    } 
    $key++; 
} // end while 
// Close the CSV file 
fclose($handle); 
} // end if 

?> 

要在CSV顯示在整列:

<?php  
     for ($row = 1; $row <$arrCSV; $row++) 
      { 
     echo ''.$arrCSV[$row]['6'].''; 
      } 
    ?>