从自动生成的数组中删除不需要的 last x 数组索引
Trimming off the not needed last x array indexes from an auto generated array
我目前用来生成数组的代码..
<?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 个)不需要的索引(例如我的示例数组中的项目 [stuff] 和 [stuff2]。
有没有办法指定一个值来隐藏最后 x 个索引?
使用 array_pop($arr);
我能够解决我的问题,当我想从末尾删除超过 1 个时,我可以为每个想要砍掉末尾的 1 个重复代码。
我目前用来生成数组的代码..
<?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 个)不需要的索引(例如我的示例数组中的项目 [stuff] 和 [stuff2]。
有没有办法指定一个值来隐藏最后 x 个索引?
使用 array_pop($arr);
我能够解决我的问题,当我想从末尾删除超过 1 个时,我可以为每个想要砍掉末尾的 1 个重复代码。