I'm trying to create an HTML using PHP associate array. My function looks like:
function generateTable($associative_array){
echo '<table><thead><tr><th>';
echo implode('</th><th>', array_keys(current($associative_array)));
echo '</th></tr></thead><tbody>';
foreach ($associative_array as $row){
array_map('htmlentities', $row);
echo '<tr valign="top"><td>';
echo implode('</td><td>', $row);
echo '</td></tr>';
}
echo '</tbody></table>';
}
AND my associative array generated is like this:
array(2) {
[0]=> array(3) {
["Objective"]=> string(21) "Product Catalog Sales"
["Targeting Group"]=> array(3) {
[0]=> string(31) "female Age 18-24 28.73% cheaper"
[1]=> string(31) "female Age 25-34 28.18% cheaper"
[2]=> string(30) "female Age 35-44 4.51% cheaper"
}
["Placement"]=> array(3) {
[0]=> string(33) "mobile_feed iphone 30.62% cheaper"
[1]=> string(45) "mobile_feed android_smartphone 19.58% cheaper"
[2]=> string(33) "right_hand desktop 21.48% cheaper"
}
}
[1]=> array(3) {
["Objective"]=> string(11) "Conversions"
["Targeting Group"]=> array(6) {
[0]=> string(31) "female Age 18-24 28.73% cheaper"
[1]=> string(31) "female Age 25-34 28.18% cheaper"
[2]=> string(30) "female Age 35-44 4.51% cheaper"
[3]=> string(31) "female Age 18-24 14.41% cheaper"
[4]=> string(31) "female Age 25-34 14.75% cheaper"
[5]=> string(30) "female Age 35-44 6.14% cheaper"
}
["Placement"]=> array(4) {
[0]=> string(33) "mobile_feed iphone 30.62% cheaper"
[1]=> string(45) "mobile_feed android_smartphone 19.58% cheaper"
[2]=> string(33) "right_hand desktop 21.48% cheaper"
[3]=> string(33) "right_hand desktop 58.77% cheaper"
}
}
}
The table structure is being generated, but it is unable to input the values, below is the error that is displayed: can someone please help how to modify the function.
Warning: htmlentities() expects parameter 1 to be string
Notice: Array to string conversion
array_map(), which does not operate on arrays by reference. So this line of code is redundant?