I am a newbie in Codeigniter and created a form to input list of website from user where user either can insert website urls in a textarea separated by line or upload csv file contain a header named websites. I am using codeigniter library CSVReader to read data from csv and create an array like this:
Array ( [0] => Array ( [websites] => www.google.com ) [1] => Array ( [websites] => www.bing.com ) )
while if I try to convert array from PHP explode function (if user input websites through textarea) Array looks like this:
Array(www.google.com,www.bing.com)
Code of my CSVReader is as:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
/**
* Parse a file containing CSV formatted data.
*
* @access public
* @param string
* @param boolean
* @return array
*/
function parse_file($p_Filepath, $p_NamedFields = true) {
$content = false;
$file = fopen($p_Filepath, 'r');
if($p_NamedFields) {
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
}
while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {
if( $row[0] != null ) { // skip empty lines
if( !$content ) {
$content = array();
}
if( $p_NamedFields ) {
$items = array();
// I prefer to fill the array with values of defined fields
foreach( $this->fields as $id => $field ) {
if( isset($row[$id]) ) {
$items[$field] = $row[$id];
}
}
$content[] = $items;
} else {
$content[] = $row;
}
}
}
fclose($file);
return $content;
}
}
Could someone please help me create array in same styles?