2

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>';
    }
7
  • can you do a var_export of your PHP array, it will be easier for us to help you then - php.net/var_export .. then edit your post with that array instead of what you originally posted. Commented Jan 8, 2016 at 7:50
  • sure. let me do that please Commented Jan 8, 2016 at 7:54
  • This can be taken as constant.. I'm actually importing that as from other array.. if you look at the generate table, it has two arrays passed as arguments. one array is used for creating the table name, and other is for data. I won't mind, if I hard code them as well Commented Jan 8, 2016 at 8:00
  • You seem to have lost the newlines in your strings in your latest edit. Commented Jan 8, 2016 at 8:01
  • 1
    Is the array you showed $associative_array or $associative_array1? Commented Jan 8, 2016 at 8:02

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

its saying undefined offset. Moreover, its creating multiple rows under one column 1.error is at this line echo "<td>" . nl2br($associative_array1[$key][$i]) . "</td>";
I don't know what I'm doing wrong. it says Notice: Undefined offset: 0, Notice: Undefined offset: 1 until ``Notice: Undefined offset: 4`
1

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);

8 Comments

let me check this.I understand, data is in unusual format, but this is the best I could send the data. the other arrays were so bad that it was taking so many nested arrays to create the html.
this is not working, moreover, just 5 rows are being printed
just "5 rows" of what is being printed out?
Do you mean the column headers?
Yeah well it looks fine to me: imgur.com/frq3iJc -- are you sure you associative array is the same as what you posted? please post how you are calling generateTable2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.