Jump to content

Access to associative array index strings


mjsulliv

Recommended Posts

I have an app that is the interface to a MySQL db. Based on inputs, it builds a table and selects what to return. The resultant array is parsed into row-arrays using “mysql_fetch_array()”. I know the returned row-array can be indexed by either numerical or associative strings; the associative strings representing the column values of the table(s) queried. What I’d like to do is use the associative index string values from the row array for the <th> value of the table being built. Is there a way to get access to these strings?Any help will be appreciated. --- Mike

Link to comment
Share on other sites

What I’d like to do is use the associative index string values from the row array for the <th> value of the table being built. Is there a way to get access to these strings?
I can think of several ways to achieve your goal, but this is probably the simplest and most direct -- the array_keys() function in conjunction with a foreach-statement.Roddy
Link to comment
Share on other sites

An even easier way is probably to use the extended form of foreach, like:

foreach($row as $columnName => $columnValue)

[edit]Oh wait... you want to have the column names in a header... I suppose using array_keys() to first form the headers, and then using the above on the actual values is indeed a better solution.[/edit]

Link to comment
Share on other sites

BTW, while the above approach is the easy way, if you want your app to also support overlapping column names (SQL may return two columns with the same name, but PHP will only show the last one with that name if you use fetch_assoc()), you can use an indexed array in combination with mysqli_result::fetch_fields() to get the name (and more) of the indexed fields.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...