skip to Main Content

I have a database with 2 tables. Via an sql query I get the (joined) info and put it into an array $rsvpData.

I now want to print this data but combine group/couples/families with the same id as one and have their individual rsvp status listed.
Basically, every person has their own name_id, groups share an group_id.

It would print something like:

Billy Joel
- Billy Joel - 1

Doe Family
- John Doe - 1
- Jane Doe - 0

etc..

So far I have a foreach loop which goes through all the id’s, keeps track of which have already been done and skips them if so.
Now I need to find all associated names with the couple id.

The code below successfully prints the group name, and if finds the right number of names per group, but the individual names are wrong/offset.
Using $v as the key is obviously wrong, I just can’t figure out what I should use here.

$alreadyProcessed = array();

foreach ($rsvpData as $k => $v) {
  if (!in_array($v["group_id"], $alreadyProcessed)) {
    echo "<p><strong>" . $v["invite_name"] . "</strong></p>";

    //Find all names/rsvpstatus
    $names = array_keys(array_column($rsvpData, 'group_id'), $v["group_id"]);
    foreach ($names as $name) {

      //Print name/rsvp status
      echo "<p>" . $v["name"] . " - " . $v["rsvp_status"] . "</p>";

      //Or maybe it's closer to this?
      echo "<p>" . $rsvpData[$v]["name"] . " - " . $rsvpData[$v]["rsvp_status"] . "</p>";
    }

    array_push($alreadyProcessed, $v["group_id"]);

    } else {
      //Already done, can skip.
    };
}

The array $rsvpData

Array
(
    [1] => Array
        (
            [group_id] => 1
            [name_id] => 1
            [invite_name] => Billy Joel
            [name] => Billy Joel
            [rsvp_status] => 1
        )

    [2] => Array
        (
            [group_id] => 2
            [name_id] => 2
            [invite_name] => Doe Family
            [name] => John Doe
            [rsvp_status] => 1
        )

    [3] => Array
        (
            [group_id] => 2
            [name_id] => 3
            [invite_name] => Doe Family
            [name] => Jane Doe
            [rsvp_status] => 0
        )

    [4] => Array
        (
            [group_id] => 3
            [name_id] => 4
            [invite_name] => Clark Kent
            [name] => Clark Kent
            [rsvp_status] => 0
        )

    [5] => Array
        (
            [group_id] => 4
            [name_id] => 5
            [invite_name] => David Smith
            [name] => David Smith
            [rsvp_status] => 2
        )

    [6] => Array
        (
            [group_id] => 5
            [name_id] => 6
            [invite_name] => Chris & Julie
            [name] => Chris
            [rsvp_status] => 1
        )

    [8] => Array
        (
            [group_id] => 5
            [name_id] => 6
            [invite_name] => Chris & Julie
            [name] => Julie
            [rsvp_status] => 0
        )
    )
)

2

Answers


  1. $k is the key. $v is the data. Try this:
    $rsvpData[$k]["name"]

    Login or Signup to reply.
  2. OK, I see the problem. You are putting the keys in $name so I think what you want is $rsvpData[$name]["name"]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search