1

Here, I m using PHPexcel library to reading the file. My excel File look like

enter image description here

Controller

class Home extends CI_Controller {
    public function index() {
        $this->load->library('excel');
        $reader = PHPExcel_IOFactory::createReader('Excel2007');
        $reader->setReadDataOnly(true);
        $file = isset($_FILES["uploadexcel"]['tmp_name']) ? $_FILES["uploadexcel"]['tmp_name'] : '';
        $excel = $reader->load($file);
        foreach ($excel->getWorksheetIterator() as $worksheet) {
            $worksheets = $worksheet->toArray();
        }
        echo '<pre>';print_r($worksheets);
    }
}

when I am uploading excel file my array like:

 Array
 (
  [0] => Array
    (
        [0] => url
        [1] => category_1
        [2] => category_2
        [3] => language
        [4] => f_n
        [5] => publishers
        [6] => cost
        [7] => cost_currency
        [8] => processing_time
        [9] => example
        [10] => note
    )
    [1] => Array
    (
        [0] => gettingaway.com
        [1] => 10
        [2] => 14
        [3] => GA
        [4] => nofollow
        [5] => 2
        [6] => 1245
        [7] => 
        [8] => 12
        [9] => testing
        [10] => Testing Value
    )


 )

But, I want the array like :

Array
   (
    [0] => Array
    (
        [url] => gettingaway.com
        [category_1] => 10
        [category_2] => 14
        [language] => GA
        [f_n] => nofollow
        [publishers] => Cust Angel
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 12
        [example] => example
        [note] => note
    )   
    [1] => Array
    (
        [url] => thebusbench.com
        [category_1] => 5
        [category_2] => 13
        [language] => GA
        [f_n] => nofollow
        [publishers] => Cust Angel
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 6
        [example] => example
        [note] => note
    )   
    [2] => Array
    (
        [url] => travelintelligence.net
        [category_1] => 6
        [category_2] => 11
        [language] => GA
        [f_n] => nofollow
        [publishers] => Cust Angel
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 2
        [example] => example
        [note] => note
    )      
 )

In above array, my publisher key value is 2. i.e. the Id of publisher into my database. I want this related id publisher name into array. In my database id 2 related publisher name is Cust Angel.

Barry
  • 3,303
  • 7
  • 23
  • 42
Puja
  • 451
  • 2
  • 5
  • 20

2 Answers2

2

If can help who ends here I have found a solution a little more elegant for the case in question:

foreach ($excel->getWorksheetIterator() as $worksheet) {
    $worksheetArray = $worksheet->toArray();

    // assuming the header is in the first line
    $header = $worksheetArray[0];
    unset($worksheetArray[0]);

    $associativeArraySheet = array_map(static function ($row) use ($header) { return array_combine($header, $row); }, $worksheetArray);

    print_r($associativeArraySheet);
}
1

Here, My question solution..

 $reader = PHPExcel_IOFactory::createReader('Excel2007');
    $reader->setReadDataOnly(true);
    $file = isset($_FILES["uploadexcel"]['tmp_name']) ? $_FILES["uploadexcel"]['tmp_name'] : '';
    $objPHPExcel = $reader->load($file);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $header=true;
    if ($header) {
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();
        $headingsArray = $objWorksheet->rangeToArray('A1:' . $highestColumn . '1', null, true, true, true);
        $headingsArray = $headingsArray[1];
        $r = -1;
        $namedDataArray = array();
        for ($row = 2; $row <= $highestRow; ++$row) {
            $dataRow = $objWorksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, true, true);
            if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
                ++$r;
                foreach ($headingsArray as $columnKey => $columnHeading) {
                    $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
                }
            }
        }
    } else {
        //excel sheet with no header
        $namedDataArray = $objWorksheet->toArray(null, true, true, true);
    }
    echo '<pre>';print_r($namedDataArray);exit;
 }

output:-

Array
 (
  [0] => Array
         (
        [url] => gettingaway.com
        [category_1] => Finance
        [category_2] => General
        [language] => GA
        [f_n] => nofollow
        [publishers] => KAlw
        [cost] => 1245
        [cost_currency] => 
        [processing_time] => 12
        [example] => testing
        [note] => Testing Value
    )

[1] => Array
    (
        [url] => thebusbench.com
        [category_1] => Books & Writing
        [category_2] => Games
        [language] => GA
        [f_n] => nofollow
        [publishers] => Miclede
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 11
        [example] => fsdfsd fsdfd
        [note] => asd
    )

[2] => Array
    (
        [url] => travelintelligence.net
        [category_1] => Finance
        [category_2] => Food & Drinks
        [language] => GA
        [f_n] => follow
        [publishers] => Micel 
        [cost] => 123
        [cost_currency] => 
        [processing_time] => 25
        [example] => fsdfsd fsdfd
        [note] => dfmlsdflsdk fjsdlfj sdlfjsdl fjld
    )

[3] => Array
    (
        [url] => littleyayas.com
        [category_1] => Automotive
        [category_2] => General
        [language] => GA
        [f_n] => follow
        [publishers] => Cust Poo
        [cost] => 10200
        [cost_currency] => 
        [processing_time] => 5
        [example] => asds
        [note] => adasdd
    )

[4] => Array
    (
        [url] => tripwheeling.com
        [category_1] => Finance
        [category_2] => Finance
        [language] => GA
        [f_n] => follow
        [publishers] => Cust Angel
        [cost] => 152000
        [cost_currency] => 
        [processing_time] => 13
        [example] => dasd
        [note] => adas
    )

[5] => Array
    (
        [url] => gettingaway.com
        [category_1] => Home & Garden
        [category_2] => General
        [language] => EN
        [f_n] => nofollow
        [publishers] => abc
        [cost] => 250
        [cost_currency] => USD
        [processing_time] => 10
        [example] => asdasd
        [note] => asd
    )

 )
Puja
  • 451
  • 2
  • 5
  • 20