将数组传递给in_array()

ptr*_*cao 0 php arrays

请考虑以下PHP:

function get_ship_class()
{
    $csv = array_map("str_getcsv", file("somefile.csv", "r")); 
    $header = array_shift($csv); 

    // Seperate the header from data
    $col = array_search("heavy_shipping_class", $header); 

    foreach ($csv as $row)
    {      
        $array[] = $row[$col]; 
    }
}
Run Code Online (Sandbox Code Playgroud)

如何将上述函数中生成的数组传递给

if( in_array() ){
    //code
}
Run Code Online (Sandbox Code Playgroud)

Nig*_*Ren 7

略微缩写的版本,但与建议的相同是从函数返回所需的数据,但array_column()用于提取数据...

function get_ship_class()
{
    $csv = array_map("str_getcsv", file("somefile.csv", "r")); 
    $header = array_shift($csv); 

    // Seperate the header from data
    $col = array_search("heavy_shipping_class", $header); 

    // Pass the extracted column back to calling method
    return array_column($csv,$col);
}
Run Code Online (Sandbox Code Playgroud)

并使用它......

if ( in_array( "somevalue", get_ship_class() )) {
   //Process 
}
Run Code Online (Sandbox Code Playgroud)

如果您要多次使用此返回值,则可能值得将其存储在变量中,而不是直接将其传递给in_array()方法.