user4943236
user4943236

Reputation: 6344

PHP array to HTML table

I'm struggling to create a code that can help me create a table from the below php array. The array name is $associative_array1

array(
'Objective' => array(0 => 'Conversions', 1 => 'Lead Generation', ),
'Gender' => array(0 => 'Male (17.99% cheaper )', 
                  1 => 'Male (6.46% cheaper )  Male (16% cheaper )', ),
'Age' => array(0 => '45-54 (17.99% cheaper )', 
               1 => '45-54 (6.46% cheaper )35-44 (16% cheaper )', ),
'Placement' => array(0 => 'Mobile Feed (30.8% cheaper) right_hand (46.81% cheaper)', 
                     1 => 'Mobile Feed (12.56% cheaper)', ),
'Device' => array(0 => 'Android (30.8% cheaper) Desktop (46.81% cheaper)',
                  1 => 'iPhone (12.56% cheaper)', ),
)

Expected Output:enter image description here

Headings can be taken as constant.

I tried creating code, however that is so bad that it wasn't worth sharing here. Basically, the code did nothing. Sorry, I'm a noob in it and need help from peers.

PHP code

 function generateTable2($associative_array,$associative_array1){
    echo '<table width="620" class="optimization_table" border="1"  cellspacing="0" cellpadding="0"><thead><tr><th colspan=2>';
    echo implode('</th><th colspan=2>', array_keys(current($associative_array)));
    echo '</th></tr></thead><tbody>';
    foreach ($associative_array1 as $row=>$value){
    echo "<td>";
    if(is_array($value))
        foreach ($value as $key =>$value2) {
            print_r($value2[$key]);
            foreach ($value2 as $value3) {

            }

            # code...
        }

    }
    echo '</tbody></table>';
    }

Upvotes: 2

Views: 1193

Answers (2)

Clay
Clay

Reputation: 4760

The data is in an unusual format. Usually you want the data already in a row-like format.

Here is what I came up with:

<?php        
$data = array ( 
    'Objective' => array ( 0 => 'Conversions', 1 => 'Lead Generation', ),
    'Gender' => array ( 0 => 'Male (17.99% cheaper )', 1 => 'Male (6.46% cheaper )  Male (16% cheaper )', ), 
    'Age' => array ( 0 => '45-54 (17.99% cheaper )', 1 => '45-54 (6.46% cheaper )35-44 (16% cheaper )', ), 
    'Placement' => array ( 0 => 'Mobile Feed (30.8% cheaper) right_hand (46.81% cheaper)', 1 => 'Mobile Feed (12.56% cheaper)', ), 
    'Device' => array ( 0 => 'Android (30.8% cheaper) Desktop (46.81% cheaper)', 1 => 'iPhone (12.56% cheaper)', ),
);

function generateTable2($associative_array,$associative_array1){
    echo '<table width="620" class="optimization_table" border="1"  cellspacing="0" cellpadding="0"><thead><tr><th colspan=1>';
    echo implode('</th><th colspan=2>',$associative_array);
    echo '</th></tr></thead><tbody>';

    $rowCount = count( current( $associative_array1 ) );
    for ($x=0; $x<$rowCount; $x++) {
        echo "<tr>";
        foreach ($associative_array1 as $key => $data){
            echo "<td>".$data[ $x ]."</td>";
        }
        echo "</tr>\n";
    }

    echo '</tbody></table>';
}

generateTable2(['Objective', 'Top Performing Targeting Group', 'Top Performing Placement'], $data);

Upvotes: 1

Barmar
Barmar

Reputation: 781300

You can't use a nested foreach loop like that, because your HTML table is pivoted from the orientation of the PHP arrays. Each row contains the same index of all the sub-arrays, so you need to do something like this to display the data rows of the table.

$keys = array_keys($associative_array1);
$count = count($associative_array1[$keys[0]]);
for ($i = 0; $i < $count; $i++) {
    echo "<tr>";
    foreach ($keys as $key) {
        echo "<td>" . nl2br($associative_array1[$key][$i]) . "</td>";
    }
    echo "</tr>";
}

DEMO

Upvotes: 2

Related Questions