英文:
PHP search array
问题
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
)
)
)
英文:
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
)
)
)
答案1
得分: 0
$k是关键字。$v是数据。尝试这样做:
$rsvpData[$k]["name"]
英文:
$k is the key. $v is the data. Try this:
$rsvpData[$k]["name"]
答案2
得分: 0
我明白了问题。你正在将键放入$name中,所以我认为你想要的是$rsvpData[$name]["name"]。
英文:
OK, I see the problem. You are putting the keys in $name so I think what you want is $rsvpData[$name]["name"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论