将逗号分隔的数组项展开为单独的数组项

时间:2015-01-20 13:18:22

标签: php arrays

我有一个CSV文件,我将其解析为关联数组,我需要将其保存在数据库中。问题是我的一些CSV列包含其他CSV值,我需要将其单独插入数据库。示例说得更好,所以这里有:

ID, Col1, Col2
1, "Example 1.1.1,Example 1.1.2", Example 1.2
2, Example 2.1, "Example 2.2.1, Example 2.2.2"

如您所见,对于第1行,第一列中有2个值,而对于第2行,第2列中有2个值。

我不知何故需要将这些扩展为4个单独的数组项,如下所示:

array(
    [0] => array( "ID" => 1, "Col1" => "Example 1.1.1", "Col2" => "Example 1.2" ),
    [1] => array( "ID" => 1, "Col1" => "Example 1.1.2", "Col2" => "Example 1.2" ),
    [2] => array( "ID" => 2, "Col1" => "Example 2.1", "Col2" => "Example 2.2.1" )
    [3] => array( "ID" => 2, "Col1" => "Example 2.1", "Col2" => "Example 2.2.2" )
)

PHP中是否有一个函数可以执行此操作,还是必须循环遍历整个事物?

1 个答案:

答案 0 :(得分:0)

方法:

  • 自行阅读CSV文件的每一行
  • 从CSV文件的第一行保存标题数组
  • 对于其他行,请检查双引号内是否有文字。
  • 将此文本保存在数组中,并从原始行字符串中删除此文本。
  • 从原始行中提取数据并填充数组并将数组附加到"主数组"如果没有重复。
  • 这是您的问题:如果有重复,请通过循环遍历双引号中的结果数组并自行添加每个结果来处理它们。

此代码应与您提供的数据一起使用,但它可能不适用于以下情况:

ID, Col1, Col2
1, "Example 1.1.1,Example 1.1.2", "Example 1.2"
2, Example 2.1, "Example 2.2.1, Example 2.2.2"

在一行中包含多个CSV字符串可能会破坏它。我没有太多时间来测试所有可能性。

以下是代码:

$file = @fopen("file.csv", "r");
$major_arr = array();
$headers_array = array();
$iterator = 0;
$total_cols = 0;
if(!empty($file)){
    while(! feof($file)){
        $major_line_arr = array();
        $line_str = fgetcsv($file);
        if($iterator == 0){
            // First Line
            // Read headers
            $headers_array = explode(",", $line_str);
            // Trim the whitespace in the array
            $headers_array = array_map('trim', $headers_array);
            $total_cols = sizeof($headers_array);
        } else {
            // Search for text within double quotes
            $text_btw_double_quotes_arr = array();
            if(preg_match('/"([^"]+)"/', $line_str, $m)){
                array_push($text_btw_double_quotes_arr, $m[1]);   
            }
            // Remove this text from the original line
            $new_line_str = $line_str;
            if(!empty($text_btw_double_quotes_arr)){
                for($i=0; $i<sizeof($text_btw_double_quotes_arr); $i++){
                    $new_line_str = str_replace($text_btw_double_quotes_arr[$i], "", $new_line_str);
                    $major_line_arr = array();
                    $double_quoted_term = $text_btw_double_quotes_arr[$i];
                    $major_line_arr_tmp = array();
                    for($j=0; $j<$total_cols; $j++){
                        array_push($major_line_arr_tmp, $csv_columns_arr[$j]);
                    }
                    // Build the Major Line array
                    if(!empty($headers_array)){
                        for($j=0; $j<sizeof($headers_array); $j++){
                            $header_title = $headers_array[$j];
                            $value = $major_line_arr_tmp[$j];
                            if($value == ''){
                                $value = $double_quoted_term;
                            }
                            $new_arr = array($header_title => $value);
                            array_push($major_line_arr, $new_arr);
                        }
                    }
                    // Add the Major Line to the Major array
                    array_push($major_arr, $major_line_arr);
                }
            } else {
                $major_line_arr_tmp = array();
                for($i=0; $i<$total_cols; $i++){
                    array_push($major_line_arr_tmp, $csv_columns_arr[$i]);
                }
                // Build the Major Line array
                if(!empty($headers_array)){
                    for($i=0; $i<sizeof($headers_array); $i++){
                        $header_title = $headers_array[$i];
                        $value = $major_line_arr_tmp[$i];
                        $new_arr = array($header_title => $value);
                        array_push($major_line_arr, $new_arr);
                    }
                }
                // Add the Major Line to the Major array
                array_push($major_arr, $major_line_arr);
            }
        }
        $iterator++;
    }
    fclose($file);
}
相关问题