如何在PHP中从两个相关的关联数组中打印元素和值

时间:2022-07-19 12:13:06

I have two associative arrays. $dpt_total is a list of departments, and the total for those departments. $cl_subtotal is a multidimensional array of department, class and subtotal.

我有两个关联数组。 $ dpt_total是一个部门列表,以及这些部门的总数。 $ cl_subtotal是一个部门,类和小计的多维数组。

I want to display the department and total, then the classes associated with that department and the subtotal for the class. For example:

我想显示部门和总数,然后显示与该部门相关联的类以及该类的小计。例如:

vdump($dpt_total);

array(2) {
'None' → float 132.88
'instore bakery' → float 786.24
}

Sizes: 2

vdump($cl_subtotal);

array(2) {
 'None' → array(1) {
 'None' → float 132.88
 }

'instore bakery' → array(10) {
  'pies' → float 70.94
  'cakes' → float 146.71
  'miscellaneous' → float 25.57
  'cookies' → float 52.38
  'brownies' → float 33.96
  'rolls' → float 143.02
  'danish' → float 90.42
  'bagels & pretzels' → float 85.68
  'breads' → float 55.73
  'dessert case' → float 81.83
 }
}

Should display these :

应该显示这些:

Department     Class     Total
--------------------------------
None                     132.88
               None      132.88
Instore Bakery           786.24
               pies      70.94
               cakes     146.71
               misc      25.57
               cookies   52.38
              (and so on)

2 个解决方案

#1


0  

hope this help.

希望这个帮助。

$tbl=array();
foreach($dpt_total as $key=>$val){
    $row=array($key,'',$val);
    $tbl[]=$row;
    $tmp=$cl_subtotal[$key];
    foreach($tmp as $key2=>val2){
        $row=array('',$key,$val);
        $tbl[]=$row;
    }
}

$table='<table><tr><th>Department</th><th>Class</th><th>Total</th></tr>'
foreach($tbl as $row){
    $table.='<tr>';
    foreach($row as $col){
        $table.='<td>'.$col.'</td>';
    }
    $table.='/<tr>';
}
$table.='</table>';

#2


2  

a simple nested foreach statement should get you what you want:

一个简单的嵌套foreach语句应该可以得到你想要的:

foreach($cl_subtotal as $department=>$classes){
    //formatting
    //also note you can use $department as a key for $dpt_total ex:
    //$dpt_total[$department] will give you the total for the department
    foreach($classes as $class=>$price){
        //formatting
    }
}

#1


0  

hope this help.

希望这个帮助。

$tbl=array();
foreach($dpt_total as $key=>$val){
    $row=array($key,'',$val);
    $tbl[]=$row;
    $tmp=$cl_subtotal[$key];
    foreach($tmp as $key2=>val2){
        $row=array('',$key,$val);
        $tbl[]=$row;
    }
}

$table='<table><tr><th>Department</th><th>Class</th><th>Total</th></tr>'
foreach($tbl as $row){
    $table.='<tr>';
    foreach($row as $col){
        $table.='<td>'.$col.'</td>';
    }
    $table.='/<tr>';
}
$table.='</table>';

#2


2  

a simple nested foreach statement should get you what you want:

一个简单的嵌套foreach语句应该可以得到你想要的:

foreach($cl_subtotal as $department=>$classes){
    //formatting
    //also note you can use $department as a key for $dpt_total ex:
    //$dpt_total[$department] will give you the total for the department
    foreach($classes as $class=>$price){
        //formatting
    }
}