如何在PHP中分解数组并使用其中的一部分
我是新手,所以非常需要你的帮助。我有一个CSV文件,我需要修改此CSV的最后一列。我所做的是使用下面的代码分隔数组中的列如何在PHP中分解数组并使用其中的一部分,php,arrays,csv,Php,Arrays,Csv,我是新手,所以非常需要你的帮助。我有一个CSV文件,我需要修改此CSV的最后一列。我所做的是使用下面的代码分隔数组中的列 <?php function readCSV($csvFile){ $file_handle = fopen($csvFile,"r"); while (!feof($file_handle)) { $line_of_text[] = fgetcsv($file_handle, 1024); } fclose($file_
<?php
function readCSV($csvFile){
$file_handle = fopen($csvFile,"r");
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
$csvFile = 'testslash.csv';
$csv = readCSV($csvFile);
print_r($csv);
?>
你是说你只需要子阵列的一部分。 使用foreach循环和新数组很容易做到这一点
$resultArray = array();
foreach($cvs as $row)
{
$resultArray[] = $row[2]; //if you dont care about the index of the array
$resultArray[$row[0]] = $row[2]; //if you want to speficy the index as the value of e.g. the first entry of the subarray;
}
尝试了
foreach
loop?请发布所需的输出。使用array\u slice()
和fgetcsv($file\u handle,1024)返回的数组使用foreach($key=>$value)编码>(即尽快处理数据),然后可以使用$value[0]、$value[1]、$value[2]
$resultArray = array();
foreach($cvs as $row)
{
$resultArray[] = $row[2]; //if you dont care about the index of the array
$resultArray[$row[0]] = $row[2]; //if you want to speficy the index as the value of e.g. the first entry of the subarray;
}