从自动生成的数组中修剪掉不需要的最后x个数组索引



我当前用于生成数组的代码。。

<?PHP
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
fgetcsv($file_handle);
fgetcsv($file_handle);
fgetcsv($file_handle);  
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}

// Set path to CSV file
$csvFile = 'ebay.csv';
$csv = readCSV($csvFile);
$arr = [];
//$csv is your array
foreach($csv as $key => $value){
if(!array_key_exists($value[0],$arr)){
$arr[$value[0]] = [];
}
$arr[$value[0]] = array_merge($arr[$value[0]],$value);  
}
foreach ($arr as $order) :
?>

它能很好地产生像这样的阵列。

Array
(
[15304] => Array
(
[0] => 15304
[1] => things1
[2] => things2
)
[15305] => Array
(
[0] => 15305
[1] => things3
[2] => things4
)
[15306] => Array
(
[0] => 15306
[1] => things5
[2] => things6
)
[stuff] => Array
(
[0] => stuff
[1] => 
[2] => 
)   
[stuff2] => Array
(
[0] => stuff2
[1] => foobar
[2] => 
)       

数组可以比我的例子有更多或更少的项,但它们总是在末尾至少有1个(有时是2个(不需要的索引(比如我的例子数组中的items[stuff]&[stuff2]

有没有一种方法可以指定一个值来隐藏最后x个索引?

使用array_pop($arr);,我能够解决我的问题,当我想从末尾删除多个1时,我可以为每个想要砍掉末尾的1重复代码。

最新更新