我目前用来生成数组的代码..
<?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个索引?
发布于 2020-03-23 19:45:07
使用array_pop($arr);,我能够解决我的问题,当我想从末尾删除超过1个的时候,我可以为每个想要砍掉末尾的1重复代码。
https://stackoverflow.com/questions/60803414
复制相似问题