PHP在循环中指定顺序并通过它指定组html输出

时间:2014-09-27 00:11:25

标签: php arrays foreach

我有一个对象数组,我希望按指定顺序在3组中输出。 现在我的输出在每3个元素之后对数据进行分组,但遗憾的是它已经很好了。 所以现在我需要按照这个顺序对它进行分组:

1组:1,4,7,10 ......等等

2组:2,5,8,11 ......等等

3组:3,6,9,12,......等等

但我无法弄清楚如何。

到现在为止,我这样做了:

<?php
$group=3;
$count = (count($this->data));
$i=0;
foreach($this->data as $key => &$result){
  $row = ( ((int)$key) % (int)$group)+1;
  if($row==1){
    echo '<div class="row">';
  }
  echo '<div class="item-'.($i+1).'">';
  /*echoing a lots of stuf*/
  echo '</div>';
  $i++;
  if($row == $group || $i == $count){
    echo '<hr />';
    echo '</div>';
  }
}
?>

但正如我之前写的那样不再好了

如果我的问题不明确,那么这是我想要的html输出:

<div class="row">
  <div class="item-1"></div>
  <div class="item-4"></div>
  <div class="item-7"></div>
  <div class="item-10"></div>
</div>
<div class="row">
  <div class="item-2"></div>
  <div class="item-5"></div>
  <div class="item-8"></div>
  <div class="item-11"></div>
</div>
<div class="row">
  <div class="item-3"></div>
  <div class="item-6"></div>
  <div class="item-9"></div>
  <div class="item-12"></div>
</div>

这甚至可能吗?

3 个答案:

答案 0 :(得分:2)

现在我真的知道你想要什么,你可以这样做:

$group = 3;

$length = count( $this->data );
$matrix = array();
for ( $i = 0, $row_n = 0; $i < $length; ++$i, $row_n++ ) {
    // Reset row.
    if ( $row_n === $group ) {
        $row_n = 0;
    }

    // Set cell.
    $row =& $matrix[$row_n];
    $row[] = 0;

    // Find column nr.
    end( $row );
    $cell_n = key( $row );

    // Update with correct key nr.
    $key = $row_n + $cell_n * $group;
    $row[$cell_n] = $key;
}

/*
echo "<pre>";
print_r( $matrix );
echo "</pre>";
*/

foreach ( $matrix as $row_n => $row ) {
    echo "\n<div class='row'>";

    foreach ( $row as $cell_n => $key ) {
        echo "\n\t<div class='item-". ($key + 1) ."'>";

        echo $this->data[$key];

        echo "\n\t</div>";
    }

    echo "\n</div>";
}

答案 1 :(得分:0)

您可以尝试这样的事情:

$data = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18);

$groups = 3;
$items_per_group = count($data) / $groups;
$i     = 0;
$item  = 0;
$row   = 0;
$open = false;

foreach ( $data as $key => &$result ) {
    if ( $i % $items_per_group === 0 ) {
        $row++;
        $item = 0;

        if ( $open ) {
            echo "\n</div>";
        }

        echo "\n<div class=\"row\">";
        $open = true;
    }

    $nr = $row + $item * $groups;
    echo "\n\t<div class=\"item-$nr\">";
    // echoing a lots of stuf
    echo "\n\t</div>";

    $item++;
    $i++;
}

if ( $open ) {
    echo "\n</div>";
}

可能会有一些错误,但我希望你能得到主旨;)

答案 2 :(得分:0)

如果您想直接执行此操作,只需将原始数组拆分为您想要的组数,然后使用新的重新排序数组打印出HTML。

$a = array(1,2,3,4,5,6,7,8,9,10,11,12);

$array = array();
$group = 3; // <--- how many groups you want

$pointer = 0; //init pointer to first group
foreach($a as $value) {
    if($pointer == $group) $pointer = 0; // reset pointer
    $array[$pointer][] = $value; // two detention array have groups -> items
    $pointer++;
}

//use new sorted array print HTMl
foreach($array as $classRow) {
    echo "<div class='row'>";
    foreach($classRow as $item) {
        echo "<div class='item-".$item."'></div>";  
    }
    echo "</div>";
}