2

I have below value in 1st Column of My CSV file

<table border="1">
<tr>
    <th align="left">First Name</th>
    <th align="left">Gender</th>
    <th align="left">Nationality</th>
</tr>
<tr>
    <td align="left">Mike</td>
    <td align="left">M</td>
    <td align="left">Singaporean</td>
</tr>

I already followed: Read each line of txt file to new array element

How to convert above CSV file to Array. So output will be

Array
(
    [0] => Array
        (
            [0] => First Name
            [1] => Gender
            [2] => Nationality
        )

    [1] => Array
        (
            [0] => Mike
            [1] => M
            [2] => Singaporean
        )
)
Community
  • 1
  • 1
Jackson
  • 1,426
  • 3
  • 27
  • 60

3 Answers3

1

HTML representation to php array using DOMDocument::saveHTML

Refer this example code:

$htmlString = '
    <td align="left">Mike</td>
    <td align="left">M</td>
    <td align="left">Singaporean</td>
';

$dom = new DOMDocument;
$dom->loadHTML($htmlString);
foreach($dom->getElementsByTagName('td') as $node)
{
    $arrayTd[] = $dom->saveHTML($node);
}

print_r($arrayTd);
Kushan
  • 10,657
  • 4
  • 37
  • 41
0

You can use any csv reading library also. For example this one - http://csv.thephpleague.com/basic-usage/ . the manipulation through libraries will be very easy

d3vdpro
  • 2,887
  • 4
  • 25
  • 29
0

you need the folde and location as param to pick the file and you can use simpleExcel lib for parsing CSV like this

function parseCSV($location,$folder){
    $excel = new SimpleExcel('CSV');

    try{
      $csv=$excel->parser->loadFile($location);
    }catch (Exception $ex){
      $logger->info($ex->getMessage());
    }

    $j = 1;
    /****column name ****/
    if($excel->parser->isRowExists($j)){
        $cols = $excel->parser->getRow($j);
    }

    $i=2;
    $arrval = array();
    /***csv data****/
    while($excel->parser->isRowExists($i)){
        $data=$excel->parser->getRow($i);
    }
}

use this function and pass required param then do var_dump($cols) and var_dump($data). this is not tested but will work

black
  • 729
  • 5
  • 13
  • 28