我有一个下面的数组,其中包含一些具有不同值的相同键。如何将相同的键值与 PHP 中的那个键结合起来?我正在使用 WAMP,Codeigniter

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

我正在通过导入 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 sheet 中的 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 文件中获取数据,那就没问题了。

谁能帮帮我。非常感谢。

您可以使用此功能解析您的 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 文件没有 header

,你应该传递 false

编辑==>

您可以在此 class 中修改几行以获得所需的结果。查看 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;  
}