Php đọc tệp csv vào mảng kết hợp

Nó có thể đọc dữ liệu CSV từ tệp, luồng đầu vào hoặc chuỗi để phân tích cú pháp và trích xuất các phần tử bản ghi

Mỗi hàng có thể được phân tích cú pháp thành một mảng kết hợp bằng cách sử dụng tên tiêu đề làm khóa hoặc một mảng thông thường với các chỉ mục được đánh số

Tôi đã thấy rất nhiều ví dụ về cách lấy tệp CSV và sau đó tạo một mảng kết hợp với các tiêu đề làm khóa

Ví dụ

Brand,Model,Part,Test
Honda,Civic,123,244
Honda,Civic,135,434
Toyota,Supra,511,664

Nơi nó sẽ tạo ra một Mảng chẳng hạn như Array[$num][$key] trong đó $key sẽ là Thương hiệu, Mẫu, Phần, Thử nghiệm

Vì vậy, nếu tôi muốn truy cập giá trị thử nghiệm "434", tôi sẽ phải lặp lại mọi chỉ mục trong mảng và sau đó bỏ qua bất kỳ Thương hiệu nào không phải là honda và bất kỳ mẫu xe nào không phải là Civic


Điều tôi cần làm là truy cập giá trị một cách trực tiếp nhất, thay vì chạy qua vòng lặp for đi qua từng chỉ mục $num. Tôi muốn có thể truy cập kiểm tra giá trị "434" với

Array['Honda']['Civic']['135']

hoặc điều khiển câu lệnh for với vòng lặp qua mọi mẫu xe Honda có… đại loại như

foreach $model in Array['Honda']

Ít nhất tôi cần có khả năng xem qua mọi mô hình được cung cấp bởi một Thương hiệu đã biết và truy cập tất cả thông tin tương đối cho từng mô hình

@normadize - that is a nice start, but it fails on situations where a field is empty but quoted (returning a string with one double quote instead of an empty string) and cases like """""foo""""" that should result in ""foo"" but instead return "foo". I also get a row with 1 empty field at the end because of the final CRLF in the CSV. Plus, I don't really like the !!Q!! magic or urlencoding to get around things. Also, \R doesn't work in pcre on any of my php installations.

Here is my take on this, without anonymous functions (so it works on PHP < 5.3), and without your options (because I believe the only correct way to parse according to the RFC would be $skip_empty_lines = false and $trim_fields = false).

//parse a CSV file into a two-dimensional array
//this seems as simple as splitting a string by lines and commas, but this only works if tricks are performed
//to ensure that you do NOT split on lines and commas that are inside of double quotes.
function parse_csv($str)
{
    //match all the non-quoted text and one series of quoted text (or the end of the string)
    //each group of matches will be parsed with the callback, with $matches[1] containing all the non-quoted text,
    //and $matches[3] containing everything inside the quotes
    $str = preg_replace_callback('/([^"]*)("((""|[^"])*)"|$)/s', 'parse_csv_quotes', $str);

________số 8

    //split on LF and parse each line with a callback
    return array_map('parse_csv_line', explode("\n", $str));
}

Array[$num][$key]0

Array[$num][$key]1

Array[$num][$key]2

Array[$num][$key]3

Array[$num][$key]4

Array[$num][$key]5

Array[$num][$key]6

Array[$num][$key]7

Array[$num][$key]8

Array[$num][$key]9

$key0

$key1

$key2

$key3

Array[$num][$key]50

Array[$num][$key]51

Array[$num][$key]52

Array[$num][$key]53