RobbTe
RobbTe

Reputation: 405

Display array in table with 2 columns

I have an array like this:

Array
(
    [0] => Array
        (
            [Title] => Title 1,
            [Value] => Value 1
        ),

    [1] => Array
        (
            [Title] => Title 2,
            [Value] => Value 2
        ),

    [2] => Array
        (
            [Title] => Title 3,
            [Value] => Value 3
        ),

    [3] => Array
        (
            [Title] => Title 4,
            [Value] => Value 4
        )

);

And I would like to create a table like this:

|---------------------|------------------|
|      Title 1        |     Title 2      |
|---------------------|------------------|
|      Value 1        |     Value 2      |
|---------------------|------------------|
|      Title 3        |     Title 4      |
|---------------------|------------------|
|      Value 3        |     Value 4      |
|---------------------|------------------|

I have found some foreach functions, but these all give me the title with the value next to it.. Any way to accomplish the above?

Upvotes: 2

Views: 3874

Answers (2)

user7941334
user7941334

Reputation:

Make use of the modulo operator, which calculates the remainder of the division of two numbers. Modulo operations are often used as solutions for processes which require repetitions with iteration steps bigger than 1 and in situations were chunking big data bundles is needed. Example: when you want to send 100 emails at once, then you loop through them and send them in packages of max. 10 per iteration step.

The principle applied in your case is: Loop through the source array and output a result (two table rows) each second iteration step, beginning with the first array key.

Version 1 - Output directly in HTML:

<?php
/*
 * Original array.
 */
$source = [
    0 => [
        'Title' => 'Title 1',
        'Value' => 'Value 1'
    ],
    1 => [
        'Title' => 'Title 2',
        'Value' => 'Value 2'
    ],
    2 => [
        'Title' => 'Title 3',
        'Value' => 'Value 3'
    ],
    3 => [
        'Title' => 'Title 4',
        'Value' => 'Value 4'
    ],
    4 => [
        'Title' => 'Title 5',
        'Value' => 'Value 5'
    ],
];

echo 'ORIGINAL:<pre>' . print_r($source, true) . '</pre>';

/*
 * Filter array for empty/null values.
 * The result is another array.
 */
$source = array_filter($source, function($el) {
    return !(
            empty($el['Title']) ||
            !isset($el['Title']) ||
            empty($el['Value']) ||
            !isset($el['Value'])
            );
});

echo 'FILTERED:<pre>' . print_r($source, true) . '</pre>';

/*
 * Reset array keys by applying array_values().
 * The result is another array.
 */
$source = array_values($source);

echo 'ARRAY_VALUES:<pre>' . print_r($source, true) . '</pre>';
?>

<table>
    <?php
    foreach ($source as $key => $item) {
        if ($key % 2 == 0) {
            ?>
            <tr>
                <td>
                    <?php
                    if (isset($source[$key]['Title'])) {
                        echo $source[$key]['Title'];
                    }
                    ?>
                </td>
                <td>
                    <?php
                    if (isset($source[$key + 1]['Title'])) {
                        echo $source[$key + 1]['Title'];
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td>
                    <?php
                    if (isset($source[$key]['Value'])) {
                        echo $source[$key]['Value'];
                    }
                    ?>
                </td>
                <td>
                    <?php
                    if (isset($source[$key + 1]['Value'])) {
                        echo $source[$key + 1]['Value'];
                    }
                    ?>
                </td>
            </tr>
            <?php
        }
    }
    ?>
</table>

Version 2 - Output from PHP:

<?php

/*
 * Original array.
 */
$source = [
    0 => [
        'Title' => 'Title 1',
        'Value' => 'Value 1'
    ],
    1 => [
        'Title' => 'Title 2',
        'Value' => 'Value 2'
    ],
    2 => [
        'Title' => 'Title 3',
        'Value' => 'Value 3'
    ],
    3 => [
        'Title' => 'Title 4',
        'Value' => 'Value 4'
    ],
    4 => [
        'Title' => 'Title 5',
        'Value' => 'Value 5'
    ],
];

echo 'ORIGINAL:<pre>' . print_r($source, true) . '</pre>';

/*
 * Filter array for empty/null values.
 * The result is another array.
 */
$source = array_filter($source, function($el) {
    return !(
            empty($el['Title']) ||
            !isset($el['Title']) ||
            empty($el['Value']) ||
            !isset($el['Value'])
            );
});

echo 'FILTERED:<pre>' . print_r($source, true) . '</pre>';

/*
 * Reset array keys by applying array_values().
 * The result is another array.
 */
$source = array_values($source);

echo 'ARRAY_VALUES:<pre>' . print_r($source, true) . '</pre>';

$tableRows = '';
foreach ($source as $key => $item) {
    if ($key % 2 == 0) {
        $tableRows .= sprintf('
                            <tr>
                                <td>
                                    %s
                                </td>
                                <td>
                                    %s
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    %s
                                </td>
                                <td>
                                    %s
                                </td>
                            </tr>
                            '
                , isset($source[$key]['Title']) ? $source[$key]['Title'] : ''
                , isset($source[$key + 1]['Title']) ? $source[$key + 1]['Title'] : ''
                , isset($source[$key]['Value']) ? $source[$key]['Value'] : ''
                , isset($source[$key + 1]['Value']) ? $source[$key + 1]['Value'] : ''
        );
    }
}

echo sprintf('<table>%s</table>', $tableRows);

Upvotes: 2

Max
Max

Reputation: 887

If your array is $array, you can use foreach as such:

echo '<table>';
foreach ($array as $row)
{
    echo "<tr>";
    echo "<td>" . $row['Title'] . "</td>";
    echo "<td>" . $row['Value'] . "</td>";
    echo "</tr>";
}
echo '</table>';

foreach basically loops through each element in $array and assigns it to $row. Since the elements of your array are other arrays, $row will be an array with the information you need for each row.

Upvotes: -1

Related Questions