Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign Two tables into php array and display the array

Tags:

php

How to assign two tables into a single array and then display it with foreach loop:

TABLE: 1

pcode       rate
------------------------------
aaa         10
bbb          5
ccc          4

TABLE: 2

 pcode       sales
------------------------------
aaa         250
bbb         100
ccc          40

I have tried so far as:

$final=array();
foreach($table1 as $t1)
        $final [$t1->code][0] = $t1->rate;

    foreach($table2 as $t2)
        $final [$t2->code][1] = $t2->sales;




    foreach ($final as $c=>$v){
        echo "$c $v[0]  $v[1]" . "\n";   \\error
    }

Please suggest?

like image 703
NKR Avatar asked Aug 10 '26 06:08

NKR


1 Answers

    $final=array();
    foreach($table1 as $t1)
            $final [$t1->code]['rate'] = $t1->rate;

        foreach($table2 as $t2)
            $final [$t2->code]['sale'] = $t2->sales;
$table='<table><tr>
<td>name</td>
<td>Sale</td>
<td>Purcahse</td>
</tr>';


    foreach ($final as $c=>$v){
$table.='<tr>
<td>'.$c.'</td>
<td>'.((isset($v['rate']))?$v['rate']:'').'</td>
<td>'.((isset($v['sale']))?$v['sale']:'').'</td>
</tr>';

    }
$table.='</table>';
like image 59
Deepesh Avatar answered Aug 12 '26 20:08

Deepesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!