我有一个下面的数组,其中一些键相同,但值不同。如何在PHP中将相同的键值与此键组合?我正在使用WAMP,Codeigniter

时间:2018-10-18 18:43:08

标签: php arrays codeigniter key key-value

我正在从导入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表中的Years和Make数据。

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

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文件中获取数据,则可以。

任何人都可以帮助我。非常感谢。

1 个答案:

答案 0 :(得分: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 ==>和<== 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;  
}