-1

I have saw the group of array like this: Imgur

And the array like this:

[1] => Array (
    [bid] => 2
    [board_name] => Test1
    [create_date] => 2019-04-25 12:28:14
    )
[2] => Array (
    [bid] => 3
    [board_name] => Test2
    [create_date] => 2019-04-25 12:28:14
    )
[3] => Array (
    [bid] => 4
    [board_name] => Test3
    [create_date] => 2019-04-25 12:28:14
    )

How can it build <tr> for every two arrays?
Like this:

<tr>
  <td>Test1</td>
  <td>Test2</td>
</tr>
<tr>
  <td>Test3</td>
</tr>

Is it count every two arrays and then group them into new array??

I'm still learning about php, maybe this question is hard to be understood what I means, sorry for my bad English..

carry0987
  • 117
  • 1
  • 2
  • 14

2 Answers2

3

I guess you need array-chunk

This is simple example:

$input_array = array('a', 'b', 'c', 'd', 'e');
$chuncks = array_chunk($input_array, 2); // contains [[a,b], [c,d], [e]]

Now you can use it to build the <tr> part as :

foreach($chuncks as $c) {
    echo "<tr><td>";
    echo implode("</td><td>" , $c);
    echo "</td></tr>";
}
dWinder
  • 11,597
  • 3
  • 24
  • 39
2

Try using array_chunk to format your array

$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));


Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
ka_lin
  • 9,329
  • 6
  • 35
  • 56