首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从自动生成的数组中修剪掉不需要的最后x个数组索引

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

Stack Overflow用户
提问于 2020-03-23 02:34:43
回答 1查看 38关注 0票数 1

我目前用来生成数组的代码..

代码语言:javascript
复制
<?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) :
 ?>

它很好地生成了像这样的数组。

代码语言:javascript
复制
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个索引?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-23 19:45:07

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60803414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档