xah icon

function create_contingency_table

xah | PRO | 09/30/20 10:18:05 PM UTC (Edited) | 0 ⭐ | 437 👁️ | Never ⏰ | []
PHP |

1.52 KB

|

None

|

0 👍

/

0 👎

<?php
 
function create_table_rows($arr, $main_arr){
    // Dunno what code it is, but ignore it. // TODO: Change to create_general_table()
}
 
/**EXAMPLE
 
$fields = array('Condition', 'Product')
create_contingency_table('Condition', 'Product', $all_stock_arr, 'Product');
 
**/
 
function create_contingency_table($var1_name, $var2_name, $main_arr, $header = ''){
    $cont_arry = array();
 
    foreach($main as $m){
        $item_code = $m['ItemCode'];
        $var1_code = $m[$var1_name];
        $var2_code = $m[$var2_name];
        $cont_code = $var1_code . $var2_code;
 
        if(! isset($cont_test) || $cont_test != $cont_code ){
            $cont_test = $cont_code;
        }
 
        $cont_arry[$item_code]['var1_code'] = $var1_code;
        $cont_arry[$item_code]['var2_code'] = $var2_code;
        $cont_arry[$item_code]['var_1_n_2'] = $cont_code;
    }
 
    echo '<table><tr><th>' . $header . '</th>';
 
    $var1_arry = array_count_values(array_column($main_arr, 'var1_code'));
    $var2_arry = array_count_values(array_column($main_arr, 'var2_code'));
    $var12_arr = array_count_values(array_column($main_arr, 'var_1_n_2'));
 
    foreach($var1_arry as $var1_key => $var1_val){
        echo '<th>' . $var1_key . '</th>';
    }
 
    echo '</tr>';
 
    foreach($var2_arry as $var2_key => $var2_val){
        echo '<tr><td>' . $var2_key . '</td>';
 
        foreach($var1_arry as $var1_key => $var1_val){
            $key_code = $var1_key . $var2_key;
 
            if(isset($var12_arr[$key_code])){
                echo '<td>' . $var12_arr[$key_code] . '</td>';}
            else{
                echo '<td></td>';
            }
        }
        echo '</tr>';
    }
    echo '</table>';
}
 

Comments