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
        )
)
2

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

this is a comment, not an answer
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.