1,105,371 Community Members

PHP array to HTML table

Member Avatar
FakeTales
Light Poster
33 posts since Jan 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
code739
Posting Whiz in Training
213 posts since May 2012
Reputation Points: 17 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 6 [?]
 
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
Member Avatar
FakeTales
Light Poster
33 posts since Jan 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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