Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中分解数组并使用其中的一部分_Php_Arrays_Csv - Fatal编程技术网

如何在PHP中分解数组并使用其中的一部分

如何在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_

我是新手,所以非常需要你的帮助。我有一个CSV文件,我需要修改此CSV的最后一列。我所做的是使用下面的代码分隔数组中的列

<?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)返回的数组$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;
}