PHP搜索数组

huangapple go评论120阅读模式
英文:

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:

  1. Billy Joel
  2. - Billy Joel - 1
  3. Doe Family
  4. - John Doe - 1
  5. - 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.

  1. $alreadyProcessed = array();
  2. foreach ($rsvpData as $k => $v) {
  3. if (!in_array($v["group_id"], $alreadyProcessed)) {
  4. echo "<p><strong>" . $v["invite_name"] . "</strong></p>";
  5. // Find all names/rsvpstatus
  6. $names = array_keys(array_column($rsvpData, 'group_id'), $v["group_id"]);
  7. foreach ($names as $name) {
  8. // Print name/rsvp status
  9. echo "<p>" . $v["name"] . " - " . $v["rsvp_status"] . "</p>";
  10. // Or maybe it's closer to this?
  11. echo "<p>" . $rsvpData[$v]["name"] . " - " . $rsvpData[$v]["rsvp_status"] . "</p>";
  12. }
  13. array_push($alreadyProcessed, $v["group_id"]);
  14. } else {
  15. // Already done, can skip.
  16. };
  17. }

The array $rsvpData

  1. Array
  2. (
  3. [1] => Array
  4. (
  5. [group_id] => 1
  6. [name_id] => 1
  7. [invite_name] => Billy Joel
  8. [name] => Billy Joel
  9. [rsvp_status] => 1
  10. )
  11. [2] => Array
  12. (
  13. [group_id] => 2
  14. [name_id] => 2
  15. [invite_name] => Doe Family
  16. [name] => John Doe
  17. [rsvp_status] => 1
  18. )
  19. [3] => Array
  20. (
  21. [group_id] => 2
  22. [name_id] => 3
  23. [invite_name] => Doe Family
  24. [name] => Jane Doe
  25. [rsvp_status] => 0
  26. )
  27. [4] => Array
  28. (
  29. [group_id] => 3
  30. [name_id] => 4
  31. [invite_name] => Clark Kent
  32. [name] => Clark Kent
  33. [rsvp_status] => 0
  34. )
  35. [5] => Array
  36. (
  37. [group_id] => 4
  38. [name_id] => 5
  39. [invite_name] => David Smith
  40. [name] => David Smith
  41. [rsvp_status] => 2
  42. )
  43. [6] => Array
  44. (
  45. [group_id] => 5
  46. [name_id] => 6
  47. [invite_name] => Chris & Julie
  48. [name] => Chris
  49. [rsvp_status] => 1
  50. )
  51. [8] => Array
  52. (
  53. [group_id] => 5
  54. [name_id] => 6
  55. [invite_name] => Chris & Julie
  56. [name] => Julie
  57. [rsvp_status] => 0
  58. )
  59. )
  60. )
英文:

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:

  1. Billy Joel
  2. - Billy Joel - 1
  3. Doe Family
  4. - John Doe - 1
  5. - 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.

  1. $alreadyProcessed = array();
  2. foreach ($rsvpData as $k =&gt; $v) {
  3. if (!in_array($v[&quot;group_id&quot;], $alreadyProcessed)) {
  4. echo &quot;&lt;p&gt;&lt;strong&gt;&quot; . $v[&quot;invite_name&quot;] . &quot;&lt;/strong&gt;&lt;/p&gt;&quot;;
  5. //Find all names/rsvpstatus
  6. $names = array_keys(array_column($rsvpData, &#39;group_id&#39;), $v[&quot;group_id&quot;]);
  7. foreach ($names as $name) {
  8. //Print name/rsvp status
  9. echo &quot;&lt;p&gt;&quot; . $v[&quot;name&quot;] . &quot; - &quot; . $v[&quot;rsvp_status&quot;] . &quot;&lt;/p&gt;&quot;;
  10. //Or maybe it&#39;s closer to this?
  11. echo &quot;&lt;p&gt;&quot; . $rsvpData[$v][&quot;name&quot;] . &quot; - &quot; . $rsvpData[$v][&quot;rsvp_status&quot;] . &quot;&lt;/p&gt;&quot;;
  12. }
  13. array_push($alreadyProcessed, $v[&quot;group_id&quot;]);
  14. } else {
  15. //Already done, can skip.
  16. };
  17. }

The array $rsvpData

  1. Array
  2. (
  3. [1] =&gt; Array
  4. (
  5. [group_id] =&gt; 1
  6. [name_id] =&gt; 1
  7. [invite_name] =&gt; Billy Joel
  8. [name] =&gt; Billy Joel
  9. [rsvp_status] =&gt; 1
  10. )
  11. [2] =&gt; Array
  12. (
  13. [group_id] =&gt; 2
  14. [name_id] =&gt; 2
  15. [invite_name] =&gt; Doe Family
  16. [name] =&gt; John Doe
  17. [rsvp_status] =&gt; 1
  18. )
  19. [3] =&gt; Array
  20. (
  21. [group_id] =&gt; 2
  22. [name_id] =&gt; 3
  23. [invite_name] =&gt; Doe Family
  24. [name] =&gt; Jane Doe
  25. [rsvp_status] =&gt; 0
  26. )
  27. [4] =&gt; Array
  28. (
  29. [group_id] =&gt; 3
  30. [name_id] =&gt; 4
  31. [invite_name] =&gt; Clark Kent
  32. [name] =&gt; Clark Kent
  33. [rsvp_status] =&gt; 0
  34. )
  35. [5] =&gt; Array
  36. (
  37. [group_id] =&gt; 4
  38. [name_id] =&gt; 5
  39. [invite_name] =&gt; David Smith
  40. [name] =&gt; David Smith
  41. [rsvp_status] =&gt; 2
  42. )
  43. [6] =&gt; Array
  44. (
  45. [group_id] =&gt; 5
  46. [name_id] =&gt; 6
  47. [invite_name] =&gt; Chris &amp; Julie
  48. [name] =&gt; Chris
  49. [rsvp_status] =&gt; 1
  50. )
  51. [8] =&gt; Array
  52. (
  53. [group_id] =&gt; 5
  54. [name_id] =&gt; 6
  55. [invite_name] =&gt; Chris &amp; Julie
  56. [name] =&gt; Julie
  57. [rsvp_status] =&gt; 0
  58. )
  59. )
  60. )

答案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"]

huangapple
  • 本文由 发表于 2023年7月11日 06:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定