如何将csv字符串(不是csv文件)安全地分解为可用的php数组

如何将csv字符串(不是csv文件)安全地分解为可用的php数组,php,arrays,string,csv,explode,Php,Arrays,String,Csv,Explode,我正在使用wordpress,需要动态填充dropdpown。我是如何在wordpress中使用fopen php函数时遇到问题的 所以我放弃了尝试导入csv文件,转而将我的csv文件内容放入wordpress选项字段中,以便快速修复 但我正在努力安全地分解csv字符串,然后使用特定列作为选项标签和值 请问我该怎么做 先谢谢你 功能 // rider nationality function motocom_rider_nationality( $field ) { // reset

我正在使用wordpress,需要动态填充dropdpown。我是如何在wordpress中使用fopen php函数时遇到问题的

所以我放弃了尝试导入csv文件,转而将我的csv文件内容放入wordpress选项字段中,以便快速修复

但我正在努力安全地分解csv字符串,然后使用特定列作为选项标签和值

请问我该怎么做

先谢谢你


功能

// rider nationality
function motocom_rider_nationality( $field )
{

    // reset choices
    $field['choices'] = array();

    // get the textarea value from options page without any formatting
    $choices = get_field('nationality_codes', 'option', false);

    // explode the value so that each line is a new array piece
    $choices = explode(",", $choices);

    // remove any unwanted white space
    $choices = array_map('trim', $choices);

    $field['choices'] = array(
        null => 'Select nationality...'
    ); 

    // loop through array and add to field 'choices'
    if( is_array($choices) )
    {

        foreach( $choices as $choice )
        {

            $label = $choice['Country'];
            $value = $choice['A4'];

            $field['choices'][ $value ] = $label;
        }
    }

    // Important: return the field
    return $field;

}
add_filter('acf/load_field/name=rider_nationality', 'motocom_rider_nationality');

选项文本区域字段中的csv字符串…

Country,A2,A3,Number
Aaland Islands,AX,ALA,248
Afghanistan,AF,AFG,4
Albania,AL,ALB,8
Algeria,DZ,DZA,12
Samoa,AS,ASM,16
Andorra,AD,AND,20


使用此解决方案修复了它

// csv to array
function motocom_csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}


// rider nationality
function motocom_rider_nationality( $field )
{

    // reset choices
    $field['choices'] = array();

    // get the textarea value from options page without any formatting
    $choices = motocom_csv_to_array( get_template_directory() . '/csv/nationality-codes.csv' );

    $field['choices'] = array(
        null => 'Select nationality...'
    ); 

    // loop through array and add to field 'choices'
    if( is_array($choices) )
    {

        foreach( $choices as $choice )
        {

            $label = $choice['Country'];
            $value = $choice['A3'];

            $field['choices'][ $value ] = $label . ' [' . $value . ']';

        }
    }

    // Important: return the field
    return $field;

}
add_filter('acf/load_field/name=rider_nationality', 'motocom_rider_nationality');

为什么不使用
str_getcsv()
?请查看我上面的评论-无法让str_getcsv将字符串拆分为正确的数组。@Joshc:您没有将输入拆分为行,而且
str_getcsv
(从输出中可以明显看出,换行符包含在输出中)<代码>str_getcsv工作正常,修复代码。同样明显的是,
str_getcsv
应该在循环中调用,但这并没有发生。@Jon,我知道代码是错误的,这就是为什么我试图找到一个有效的方法。