get_contents( $file ); if ( false === $contents ) { return $csv_data; } // Split content into lines $lines = explode( "\n", $contents ); if ( empty( $lines ) ) { return $csv_data; } // Parse header row $headers = str_getcsv( array_shift( $lines ) ); if ( ! $headers ) { return $csv_data; } $header_count = count( $headers ); foreach ( $lines as $line ) { // Skip empty lines if ( empty( trim( $line ) ) ) { continue; } $data = str_getcsv( $line ); if ( false === $data ) { continue; } $row = []; for ( $i = 0; $i < $header_count; $i++ ) { $row[$headers[$i]] = isset( $data[$i] ) ? $data[$i] : ''; } $csv_data[] = $row; } return $csv_data; } }