I have two seperate arrays I am using in my php page.
The first one holds all of the field names that I will be using to create my html table headers on the UI.
The array of data for this looks like so:
Array
(
[0] => Array
(
[fieldID] => 2
[fieldName] => Project Title
[fieldAlias] => initiativeTitle
)
[1] => Array
(
[fieldID] => 4
[fieldName] => Project Description (preview)
[fieldAlias] => initiativeDescriptionPreview
)
)
Next, I have a data set of all the records I need to print to the table. The key in this array matches the fieldAlais from the header array.
My goal here is to loop over the header array and get the fieldAlias, then loop over the data and when the fieldAlias from the header row matches a the key in the data row, it prints it out.
Here is how I populate the header array:
$primaryArray = Array();
if(isset($dashboardDetails->results->primary->fields)){
foreach($dashboardDetails->results->primary->fields as $p){
$primaryArray[] = array(
'fieldID' => (int)$p->fieldID,
'fieldName' => (string)$p->fieldName,
'fieldAlias' => (string)$p->alias
);
}
}
This is an example of the data object:
SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[initiativeDescriptionPreview] => This is a test description
[initiativeTitle] => Test
)
Here is the mess I am working with on the HTML table:
<table class="table table-hover table-striped">
<thead>
<tr>
<?php
// Loop over the primary fields
for ($i = 0; $i < count($primaryArray); ++$i) {
echo '<th class="small">'.$primaryArray[$i]['fieldName'].'</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
// For each field in our primary array
for ($i = 0; $i < count($primaryArray); ++$i) {
// Set our alais
$a = $primaryArray[$i]['fieldAlias'];
echo '<tr>';
// Loop over all of the records
foreach($dashboard->data as $value){
foreach($value as $key => $val){
if($key == $a){
echo '<td class="small">'.$val.'</td>';
}
}
}
echo '</tr>';
}
?>
</tbody>
</table>
The result of this is that its printing two rows of data when this should be the same row:
The short end of this is: I have two separate objects, headers and data. I need to print the table headers and then print the data from the other array to its corresponding header.


fieldAliassupposed to match -initiativeTitle?