【问题标题】:How to use nested loop in this example?在这个例子中如何使用嵌套循环?
【发布时间】:2019-10-16 12:16:23
【问题描述】:

考虑以下$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。我想对数据进行排序和显示,以便每个代码只有一行,然后在该行中,在等于月份的列号中显示 [cost] 的值。例如:

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'];
        }

    }
}

问题/疑问: 我是否需要在 for 循环中放置另一个循环来检查 $data['code'] == $code 是否存在?或者我该怎么做?

【问题讨论】:

  • 你能发布一个所需输出数组的例子吗?
  • @Reqven 我刚刚在上面发布了所需的输出示例。基本上我想为每个代码每个月(1-12)运行一个循环,如果数组有该代码和月份的数据,那么就像我写的那样在表格中打印它。
  • 那么你当前的代码输出正确的数据吗?
  • @u_mulder 不,这就是我发布问题的原因。如果条件不匹配,我不确定在上面的 for 循环中是否需要另一个循环。

标签: php loops logic


【解决方案1】:

我会使用一个循环来重新索引数组:

$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";
}

我真的不喜欢循环中的 if()s 的想法。

【讨论】:

  • 非常感谢。我认为echo '&lt;td&gt;', ($item[$code][.. 应该是$list[..
【解决方案2】:

我建议您先将数据预处理为结构化数组,然后将其打印出来,例如,由具有如下结构的代码索引:

[
  ['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>';
}

哪些输出:

&lt;table border="1"&gt;&lt;tr&gt;&lt;td&gt;Column&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Name 1&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;100&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Name 2&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;120&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;180&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2021-11-24
    • 2016-05-03
    • 1970-01-01
    相关资源
    最近更新 更多