-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..

0

2 Answers 2

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>";
}
Sign up to request clarification or add additional context in comments.

5 Comments

Does not really help the OP build the HTML Table though
Probably, but that was the main part of the question How can it build <tr> for every two arrays?
@dWinder Yes I can generate html table by myself, I just want to know the logic about grouping every 2 arrays
@carry0987 As I said - you can use the array_chunk for that as: array_chunk(array_column($array, "board_name"))
@dWinder I'm trying not to use implode, since this function is hard for me to insert into my code, but the logic is right, thanks a lot !!
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
        )

)

2 Comments

Does not really help the OP build the HTML Table though
Holy #$%^#$(%^#$ I cannot believe I've lived this long without this function :'(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.