Php 在本例中如何使用嵌套循环?
考虑以下Php 在本例中如何使用嵌套循环?,php,loops,logic,Php,Loops,Logic,考虑以下$data数组: Array ( [0] => Array ( [code] => 20 [name] => Name 1 [month] => 4 [cost] => 100 .. .. ) [1] => Array (
$data
数组:
Array
(
[0] => Array
(
[code] => 20
[name] => Name 1
[month] => 4
[cost] => 100
..
..
)
[1] => Array
(
[code] => 30
[name] => Name 2
[month] => 3
[cost] => 120
..
..
)
[1] => Array
(
[code] => 30
[name] => Name 2
[month] => 6
[cost] => 180
..
..
)
..
..
)
每个数组可以有未知数量的代码。每个代码都有不同的月份ID。我想对数据进行排序和显示,以便每个代码只有一行,然后在该行中,在与月份相等的列号中显示[成本]
的值。例如:
Column 1 2 3 4 5 6 7 8 9 10 11 12
Name 1 100
Name 2 120 180
以下是我正在尝试的:
为了找出应该打印多少行,我获取唯一的代码值:
$codes = array();
foreach( $data as $row ){
if ( in_array($row['code'], $codes) ) {
continue;
}
$codes[] = $row['code'];
}
接下来,我使用循环打印行:
foreach( $codes as $code ){
//print 12 columns for each othe row
for ($i = 1; $<=12; $i++) {
//display column value if the code is same as the row
//and the month value is same as $i
//here's the problem
if (( $data['code'] == $code ) &&( $data['month'] == $i )) {
echo $data['cost'];
}
}
}
foreach($code作为$code){
//每行打印12列
对于($i=1;$,我建议您先将数据预处理为结构化数组,然后再打印它,例如,按如下结构的代码编制索引:
[
['the code'] => [
'name' => '',
'months' => [
'6' => // cost
]
]
]
您可以这样做:
$items = [
[
'code' => 20,
'name' => 'Name 1',
'month' => 4,
'cost' => 100,
],
[
'code' => 30,
'name' => 'Name 2',
'month' => 3,
'cost' => 120,
],
[
'code' => 30,
'name' => 'Name 2',
'month' => 6,
'cost' => 180,
],
];
$sortedItems = [];
foreach ($items as $item) {
if (!isset($sortedItems[$item['code']])) {
$sortedItems[$item['code']] = [
'name' => $item['name'],
'months' => [],
];
}
$sortedItems[$item['code']]['months'][$item['month']] = $item['cost'];
}
您可以使用类似以下功能打印:
function printItemsOf(array $list) {
echo '<table>';
// print header
echo '<tr>';
echo '<td>Column</td>';
for ($i = 1; $i <= 12; $i++) {
echo '<td>' . $i . '</td>';
}
echo '</tr>';
// print items
foreach ($list as $code => $item) {
echo '<tr>';
echo '<td>' . $item['name'] . '</td>';
for ($i = 1; $i <= 12; $i++) {
if (!isset($item['months'][$i])) {
echo '<td></td>';
} else {
echo '<td>' . $item['months'][$i] . '</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
函数printItemsOf(数组$list){
回声';
//打印页眉
回声';
回声“柱”;
对于($i=1;$i$项目){
回声';
回显'.$item['name'].';
对于($i=1;$i),我建议您先将数据预处理为结构化数组,然后再打印它,例如,通过具有如下结构的代码进行索引:
[
['the code'] => [
'name' => '',
'months' => [
'6' => // cost
]
]
]
您可以这样做:
$items = [
[
'code' => 20,
'name' => 'Name 1',
'month' => 4,
'cost' => 100,
],
[
'code' => 30,
'name' => 'Name 2',
'month' => 3,
'cost' => 120,
],
[
'code' => 30,
'name' => 'Name 2',
'month' => 6,
'cost' => 180,
],
];
$sortedItems = [];
foreach ($items as $item) {
if (!isset($sortedItems[$item['code']])) {
$sortedItems[$item['code']] = [
'name' => $item['name'],
'months' => [],
];
}
$sortedItems[$item['code']]['months'][$item['month']] = $item['cost'];
}
您可以使用类似以下功能打印:
function printItemsOf(array $list) {
echo '<table>';
// print header
echo '<tr>';
echo '<td>Column</td>';
for ($i = 1; $i <= 12; $i++) {
echo '<td>' . $i . '</td>';
}
echo '</tr>';
// print items
foreach ($list as $code => $item) {
echo '<tr>';
echo '<td>' . $item['name'] . '</td>';
for ($i = 1; $i <= 12; $i++) {
if (!isset($item['months'][$i])) {
echo '<td></td>';
} else {
echo '<td>' . $item['months'][$i] . '</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
函数printItemsOf(数组$list){
回声';
//打印页眉
回声';
回声“柱”;
对于($i=1;$i$项目){
回声';
回显'.$item['name'].';
对于($i=1;$i我将使用一个循环来重新索引数组:
$list = $codes = [];
for ($array as $item){
$list[$item['code']][$item['month']] = $item;
$codes[$item['code']] = $item['name'];
}
然后使用两个循环进行打印:
for ($codes as $code => $codeName){
echo '<tr><th>', $codeName, '</th>';
for (range(1, 12) as $month){
echo '<td>', ($list[$code][$month]['cost'] ?? ''), '</td>;
}
echo '</tr>', "\n";
}
for($code作为$code=>$code名称){
回显“”,$codeName“”;
用于(范围(1,12)为$month){
回显“”,($list[$code][$month]['cost']??“”);
}
回显“”,“\n”;
}
我真的不喜欢在循环中使用if()的想法。我会使用一个循环来重新索引数组:
$list = $codes = [];
for ($array as $item){
$list[$item['code']][$item['month']] = $item;
$codes[$item['code']] = $item['name'];
}
然后使用两个循环进行打印:
for ($codes as $code => $codeName){
echo '<tr><th>', $codeName, '</th>';
for (range(1, 12) as $month){
echo '<td>', ($list[$code][$month]['cost'] ?? ''), '</td>;
}
echo '</tr>', "\n";
}
for($code作为$code=>$code名称){
回显“”,$codeName“”;
用于(范围(1,12)为$month){
回显“”,($list[$code][$month]['cost']??“”);
}
回显“”,“\n”;
}
我真的不喜欢在循环中使用if()的想法。你能发布一个所需输出数组的示例吗?@Reqven我刚刚发布了上面所需的输出示例。基本上我希望每个月运行一个循环(1-12)对于每个代码,如果数组中有该代码和月份的数据,则只需像我在问题中所写的那样将其打印在表中。那么您当前的代码是否输出了正确的数据?@u_mulder否,这就是我发布该问题的原因。如果条件不匹配,我不确定是否需要在上面的for循环中添加另一个循环。您能否发布所需输出的示例put array?@Reqven我刚刚发布了上面所需的输出示例。基本上我希望每个月运行一个循环(1-12)对于每个代码,如果数组中有该代码和月份的数据,则只需像我在问题中所写的那样将其打印在表中。那么您当前的代码是否输出正确的数据?@u\u mulder否,这就是我发布该问题的原因。如果条件不匹配,我不确定是否需要在上面的for循环中使用另一个循环。非常感谢。我认为echo',($item[$code][..
应该是$list[..
非常感谢。我认为echo',($item[$code][..
应该是$list[..