-1

I have a multidimensional array and I would like display the values in a table.

Here is the code:

$attributes = [
    [
        "attributeName"=> "Name",
        "value"=> "Paul"
    ],
    [
        "attributeName"=> "Name",
        "value"=> "Steve"
    ],
    [
        "attributeName"=> "Name",
        "value"=> "John"
    ],
    [
        "attributeName"=> "Name",
        "value"=> "Andrew"
    ],

    [
        "attributeName"=> "Destination",
        "value"=> "London"
    ],
    [
        "attributeName"=> "Destination",
        "value"=> "Paris"
    ],
    [
        "attributeName"=> "Destination",
        "value"=> "Ankara"
    ],
    [
        "attributeName"=> "Destination",
        "value"=> "Kuwait"
    ],
];

I would like the output to be on a table with the thead being Name And Destination and the while the td being the values of Name and Destinations.

Or this

Name: Paul,Steve,John,Andrew

Destination: London,Paris,Ankara,Kuwait

Prince
  • 117
  • 1
  • 9
  • @Nick, why do you have to redirect me to a question that is totally confusing which is completely different. I have gone through the link and I do not understand any thing there. Please I need to see the answer someone drop on my question – Prince Dec 22 '20 at 01:14
  • You can as well answer my question or show my the answer someone drop. This is really frustrating. – Prince Dec 22 '20 at 01:15
  • Actually it was my answer and I deleted it because I felt the question is a duplicate. I've undeleted the answer, take a look and see what you think. – Nick Dec 22 '20 at 01:19
  • Note also that I changed the duplicate target - I think it is a much better one than the question was originally closed with. Again, please take a look at that and let me know what you think? – Nick Dec 22 '20 at 01:20
  • Thank you Nick. I really appreciate. Actually what I want is for it to be printed in table format but since I am having difficult showing the table on Stackflow editor I decided go the simple form. Thank you once again for reopening it – Prince Dec 22 '20 at 01:25

1 Answers1

0

You can use array_reduce to group all the attributes into an output array:

$output = array_reduce($attributes, function ($carry, $arr) {
    $carry[$arr['attributeName']][] = $arr['value'];
    return $carry;
}, array());

Output:

Array
(
    [Name] => Array
        (
            [0] => Paul
            [1] => Steve
            [2] => John
            [3] => Andrew
        )
    [Destination] => Array
        (
            [0] => London
            [1] => Paris
            [2] => Ankara
            [3] => Kuwait
        )
)

To output that as a table, you can use a foreach loop:

echo "<table>\n";
foreach ($output as $key => $values) {
    echo "<tr><td>$key</td><td>" . implode('</td><td>', $values) . "</td></tr>\n";
}
echo '</table>';

Output:

<table>
<tr><td>Name</td><td>Paul</td><td>Steve</td><td>John</td><td>Andrew</td></tr>
<tr><td>Destination</td><td>London</td><td>Paris</td><td>Ankara</td><td>Kuwait</td></tr>
</table>

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • @Prince actually the table isn't in the right orientation I don't think, the data is horizontal instead of vertical – Nick Dec 22 '20 at 01:33
  • Yes. I just modified it on my code. But your answer is the solution to my question. I just modified your table output to get the perfect table I am looking for. Thanks – Prince Dec 22 '20 at 01:35