1.11M Members

PHP array to HTML table

 
0
 

basically i am using xpath for a search query, the results display like this

Array ( [0] => SimpleXMLElement Object ( [TITLE] => Empire Burlesque [ARTIST] => Bob Dylan [COUNTRY] => USA [COMPANY] => Columbia [PRICE] => 10.90 [YEAR] => 1985 ) 
[1] => SimpleXMLElement Object ( [TITLE] => Greatest Hits [ARTIST] => Dolly Parton [COUNTRY] => USA [COMPANY] => RCA [PRICE] => 9.90 [YEAR] => 1982 ) 
[2] => SimpleXMLElement Object ( [TITLE] => Hard rains gonna fall [ARTIST] => Bob Dylan [COUNTRY] => USA [COMPANY] => Columbia [PRICE] => 5.50 [YEAR] => 1962 ) )

is it possible to get this displaying in a table so the header rows would show: Title, Artist, Country, Company, Price , Year

 
1
 

assumint that the variable name of your array above is $array

$index = 1;
echo '<table>';
foreach($array as $val){
    if($index==1){
        echo '<tr>';
        foreach($val as $key => $value ){
            echo '<th>'.$key.'</th>';
        }
        echo '<tr>';
    }
        echo '<tr>';
        foreach($val as $key => $value){
            echo '<td>'.$value.'</td>';
        }
        echo '</tr>';
    $index++;

}
echo '</table>';
Question Answered as of 1 Year Ago by code739
 
0
 

Brilliant i managed to get the actual data into rows but was still trying to figure out how to get the header data in thanks code739

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article