2

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?

3
  • Column 1, row 2: is "Value 2" correct? Should it be "Value 1", right? Commented Jul 4, 2017 at 16:24
  • you can nest another table in a cell where you render title in upper row and the value in next row. Commented Jul 4, 2017 at 16:24
  • Split items in chunks of size 2. Commented Jul 4, 2017 at 16:30

2 Answers 2

2

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

11 Comments

Awesome! Thanks for your help on this one, precisely what i needed (Y)
@RobbTe Well, I'm glad it worked. You are welcome. Tell me please, what does it mean "(Y)"? :-)
Ah sorry, i meant; thumbs up:)
@RobbTe Why "sorry"? Au contraire, my friend: write (Y) as many as possible ;-)))
@RobbTe Done updating with php version also. All final now. Bye.
|
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.