0

I have an array in PHP that looks like this... it's sorted in ascending order by the 'generation' value

Array
(
    [0] => Array
        (
            [userID] => 1
            [userName] => Grandfather
            [generation] => 1
        )

    [1] => Array
        (
            [userID] => 2
            [userName] => Father
            [generation] => 2
        )

    [2] => Array
        (
            [userID] => 3
            [userName] => Son
            [generation] => 3
        )

    [3] => Array
        (
            [userID] => 4
            [userName] => Daughter
            [generation] => 3
        )

)

I want to create an HTML table that looks like this...

enter image description here

One row for each generation, with each member of the generation seperated by a comma.

I've tried multiple versions of 'foreach' and 'while' solutions but haven't been able to get it right.

This is the closest I have come to success...

    echo '<table>';
    $x = 1;
    foreach ($usersArray as $subArray){
        if($subArray['generation'] == $x){
            echo '<tr>';
            echo '<td>'.$subArray['userName'].'</td>';
            echo '</tr>';
        }
        $x++;
    }
    echo '</table>';

This code however will only print the first member of each generation giving a table of...

enter image description here

Any help is appreciated.

4
  • It would help if you show your best attempt with code, then we can fix it, rather than having to start from scratch. This is a fairly common scenario though, if you generalise it. You just need a variable which keeps track of the previous "generation" value (from the last iteration of the loop). If the previous value matches the current one, you append the value to the current table row. If they don't match, start a new table row. Commented Jun 9, 2022 at 9:24
  • Thanks for responding. I have tried that but unfortunately I either end up with the 2nd person in a generation in a new row (ie: everybody in their own row) or I end up with only 1 person per generation in a row. It's not the computer, it's 100% me messing up. EDIT: I have around 10 different attempts, i'll go though them and post my best effort (nearest to the desired result) Commented Jun 9, 2022 at 9:26
  • Well that's why you need to show us your code, then we can fix it for you :-). Please edit your question to show that code and include the explanation you've just put into your comment. Commented Jun 9, 2022 at 9:27
  • I've posted the closest I've come so far. Thanks for the advice. Commented Jun 9, 2022 at 9:37

1 Answer 1

1

You could create a new array grouped by generation. And then loop through that and implode each value for row data.

<?php

$items = 
[
    [

        'name' => 'Grandfather',
        'gen'  => 1
    ],
    [
        'name' => 'Father',
        'gen'  => 2
    ],
    [
        'name' => 'Son',
        'gen'  => 3
    ],
    [
        'name' => 'Daughter',
        'gen'  => 3
    ]
];

foreach ($items as $item) {
    $gens[$item['gen']][] = $item['name'];
}

var_export($gens);

print_r(
    array_map(
        function($item) {
            return implode(', ', $item);
        }, 
        $gens
    )
);

Output:

array (
  1 => 
  array (
    0 => 'Grandfather',
  ),
  2 => 
  array (
    0 => 'Father',
  ),
  3 => 
  array (
    0 => 'Son',
    1 => 'Daughter',
  ),
)Array
(
    [1] => Grandfather
    [2] => Father
    [3] => Son, Daughter
)

You could skip the array_map above and just loop and implode, here's an example of spitting out rows:

foreach($gens as $gen) {
    echo '<tr><td>', implode(', ', $gen), '</td></tr>';
};
Sign up to request clarification or add additional context in comments.

5 Comments

I'm going to try this approach. Thank you very much Progrock.
@DivFellaPoor I think this is better than what I was proposing actually, and simpler to implement
Thanks ADyson. I appreciate the guidance. I now have the array with the Son & Daughter grouped in the 3rd Generation as in Progrock's example. Now I've just got to while/foreach them into to a table if I'm correctly understanding.
@DivFellaPoor I've added simple row generation code above. That should give you an idea.
@Progrock Thank you for taking the time out to do that, I appreciate it. I ended up using your foreach to give me the $gens array grouped by generation, then for each group I could use count($gen) so that I could echo "userName, " for all users of that generation and "userName" if it's the last user in the generation, giving me the desired effect. Again, thank you... this was one of the tasks I've been trying to solve on/off for the past 6 months.

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.