【问题标题】:I have a below array with some same keys with different values. How can I combine same keys values with that key in PHP? I am using WAMP, Codeigniter我有一个下面的数组,其中一些相同的键具有不同的值。如何在 PHP 中将相同的键值与该键组合?我正在使用 WAMP,Codeigniter
【发布时间】:2018-10-18 18:43:07
【问题描述】:

我通过导入 CSV 文件获取这些数据。在 CSV 文件中,我的第一列是 Years,所有其他列都是 Make。我正在使用 csvreader 库来解析 CSV 文件中的数据。

我的 CSVReader 库。

class CI_Csvreader {

    var $fields;        /** columns names retrieved after parsing */  
    var $separator = ',';    /** separator used to explode each line */  

    /** 
     * Parse a text containing CSV formatted data. 
     * 
     * @access    public 
     * @param    string 
     * @return    array 
     */  
    function parse_text($p_Text) {  
            $lines = explode("\n", $p_Text);  
            return $this->parse_lines($lines);  
    }  

    /** 
     * Parse a file containing CSV formatted data. 
     * 
     * @access    public 
     * @param    string 
     * @return    array 
     */  
    function parse_file($p_Filepath) {  
            $lines = file($p_Filepath);  
            return $this->parse_lines($lines);  
    }  
    /** 
     * Parse an array of text lines containing CSV formatted data. 
     * 
     * @access    public 
     * @param    array 
     * @return    array 
     */  
    function parse_lines($p_CSVLines) {  
            $content = FALSE;  
            foreach( $p_CSVLines as $line_num => $line ) {  
                    if( $line != '' ) { // skip empty lines  
                            $elements = explode($this->separator, $line);  

                            if( !is_array($content) ) { // the first line contains fields names  
                                    $this->fields = $elements;  
                                    $content = array();  
                            } else {  
                                    $item = array();  
                                    foreach( $this->fields as $id => $field ) {  
                                            if( isset($elements[$id]) ) {  
                                                    $item[$field] = $elements[$id];  
                                            }  
                                    }  
                                    $content[] = $item;  
                            }  
                    }  
            }  
            return $content;  
    }

我的 CSV 文件数据 =>

Years   Make    Make    Make
2001    Acura   Honda   Toyota
2002    Acura   Honda   
2003    Acura           Toyota
2004                          

在上述文件中的年份和 Excel/CSV 表中的数据可以稍后更改。

我的输出是一个数组。=>

Array
(
    [0] => Array
        (
            [Years] => 2001
            [Make] => Acura
            [Make] => Honda
            [Make] => Toyota
        )

    [1] => Array
        (
            [Years] => 2002
            [Make] => Acura
            [Make] => Honda
            [Make] => 
        )
    [2] => Array
        (
            [Years] => 2003
            [Make] => Acura
            [Make] => 
            [Make] => Toyota
        )
    [3] => Array
        (
            [Years] => 2004
            [Make] => 
            [Make] => 
            [Make] => 
        )
)

我想要这样的结果数组 => 我想保留空值。

Array
(
    [0] => Array
        (
            [Years] => 2001
            [Make] => Array(
                              [0]=>Acura
                              [1]=>Honda
                              [2]=>Toyota
                           )
        )

    [1] => Array
        (
            [Years] => 2002
            [Make] => Array(
                              [0]=>Acura
                              [1]=>Honda
                              [2]=>
                           )
        )
    [2] => Array
        (
            [Years] => 2003
            [Make] => Array(
                              [0]=>Acura
                              [1]=>
                              [2]=>Toyota
                           )
        )
    [3] => Array
        (
            [Years] => 2004
            [Make] => Array(
                              [0]=>
                              [1]=>
                              [2]=>
                           )
        )
)

另外请告诉我如何获得没有空值的结果。

如果有任何其他方法可以以我想要的格式从 CSV 文件中获取数据,那也可以。

谁能帮帮我。非常感谢。

【问题讨论】:

    标签: php arrays codeigniter key key-value


    【解决方案1】:

    您可以使用此功能来解析您的 CSV 文件

    function CSV_parse($string='', $has_header=true, $row_delimiter=PHP_EOL, $delimiter = "," , $enclosure = '"' , $escape = "\\" )
    {
        $rows = array_filter(explode($row_delimiter, $string));
        $firstline = true;
        $data = array();
    
        foreach($rows as $row)
        {
            if($firstline && $has_header)
            {
                $firstline=false;
                continue;
            }
    
            $row = str_getcsv ($row, $delimiter, $enclosure , $escape);
    
    
            $dtrow=array('Years'=>$row[0], 'Makes'=>array());
            for($i=1;$i<count($row);$i++)
            {
                $make=trim($row[$i]);
                if($make=='')continue;
                $dtrow['Makes'][]=$make;
            }
            $data[] =$dtrow;
    
        }
    
        return $data;
    }
    

    您应该将 CSV 文件内容作为第一个参数传递。 对于第二个参数,如果你的 csv 文件没有标题,你应该传递 false

    编辑 ==>

    您可以只修改这个类中的几行来达到预期的效果。看看 PTK ==> 和

    class CI_Csvreader {
    
        var $fields;        /** columns names retrieved after parsing */  
        var $separator = ',';    /** separator used to explode each line */  
    
        /** 
         * Parse a text containing CSV formatted data. 
         * 
         * @access    public 
         * @param    string 
         * @return    array 
         */  
        function parse_text($p_Text) {  
                $lines = explode("\n", $p_Text);  
                return $this->parse_lines($lines);  
        }  
    
        /** 
         * Parse a file containing CSV formatted data. 
         * 
         * @access    public 
         * @param    string 
         * @return    array 
         */  
    function parse_file($p_Filepath) {  
            $lines = file($p_Filepath);  
            return $this->parse_lines($lines);  
    }  
    /** 
     * Parse an array of text lines containing CSV formatted data. 
     * 
     * @access    public 
     * @param    array 
     * @return    array 
     */  
    function parse_lines($p_CSVLines) {  
            $content = FALSE;  
            foreach( $p_CSVLines as $line_num => $line ) {  
                    if( $line != '' ) { // skip empty lines  
                            $elements = explode($this->separator, $line);  
    
                            if( !is_array($content) ) { // the first line contains fields names  
                                    $this->fields = $elements;  
                                    $content = array();  
                            } else {  
    
                                    //PTK: ==> new code
                                    $item=array('Years'=>$elements[0], 'Makes'=>array());
                                    for($i=1;$i<count($elements);$i++)
                                    {
                                        $make=trim($elements[$i]);
                                        //if($make=='')continue;  //PTK: if you want to remove empty lines uncoment this line, but in this case your array may will have rows with different     lengthes
                                        $item['Makes'][]=$make;
                                    }
                                    //PTK: <== new code
                                    /* 
                                    //PTK ==> original code
                                    $item = array();  
                                    foreach( $this->fields as $id => $field ) {  
                                            if( isset($elements[$id]) ) {  
                                                    $item[$field] = $elements[$id];  
                                            }  
                                    } 
                                    //PTK <== original code
                                    */ 
                                    $content[] = $item;  
                            }  
                    }  
            }  
            return $content;  
    }
    

    【讨论】:

    • 嗨,帕蒂。我已经用我正在使用的 CSVReader 库文件编辑了我的问题。你能告诉我在哪里可以使用你的函数 CSV_parse 吗?我必须将哪些数据作为 CSV 文件内容传递?
    • 嗨,Viren,我添加了新代码。见编辑字后
    • 嗨,帕蒂。您的新编辑代码可以根据我想要的输出结果完美运行。非常感谢您的帮助。现在我没有 15 个声望来支持你的答案。但是当我有 15 个声望时,我一定会支持你的答案。上帝保佑你。
    猜你喜欢
    • 2015-01-04
    • 2014-03-12
    • 1970-01-01
    • 2017-07-14
    • 2018-06-30
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多