dongliu4320 2014-02-15 11:11
浏览 26
已采纳

数组中的PHP csv reader关系

i have country list csv with its code i use below code to read the csv

function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;
}

and i got result like

Array
(
    [0] => AD
    [1] => Andorra
    [2] => Andorre
)
Array
(
    [0] => AE
    [1] => United Arab Emirates
    [2] => Émirats arabes unis
)
Array
(
    [0] => AF
    [1] => Afghanistan
    [2] => Afghanistan
)
Array
(
    [0] => AG
    [1] => Antigua and Barbuda
    [2] => Antigua-et-Barbuda
)
Array
(
    [0] => AI
    [1] => Anguilla
    [2] => Anguilla
)

i want to build some relation ship like if write "Anguilla" in text box i get its cod "AI" and so on for each but could not figure out how to make relationship between them for key 0 and key 1

  • 写回答

1条回答 默认 最新

  • drz5553 2014-02-15 11:18
    关注

    You can achieve this using associative arrays.

    Replace

    $line_of_text[] = fgetcsv($file_handle, 1024);
    

    With

    $line = fgetcsv($file_handle, 1024);
    $line_of_text[$line[1]] = $line;
    

    This way your function will return something like:

    Array
    (
        [Andorra] => Array
            (
                [0] => AD
                [1] => Andorra
                [2] => Andorre
            )
    
        [United Arab Emirates] => Array
            (
                [0] => AE
                [1] => United Arab Emirates
                [2] => Émirats arabes unis
            )
    
        ....
    )
    

    So you will be able to get the row for Anguilla using $line_of_text['Anguilla'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路