PHP搜索数组

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

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 =&gt; $v) {
if (!in_array($v[&quot;group_id&quot;], $alreadyProcessed)) {
echo &quot;&lt;p&gt;&lt;strong&gt;&quot; . $v[&quot;invite_name&quot;] . &quot;&lt;/strong&gt;&lt;/p&gt;&quot;;
//Find all names/rsvpstatus
$names = array_keys(array_column($rsvpData, &#39;group_id&#39;), $v[&quot;group_id&quot;]);
foreach ($names as $name) {
//Print name/rsvp status
echo &quot;&lt;p&gt;&quot; . $v[&quot;name&quot;] . &quot; - &quot; . $v[&quot;rsvp_status&quot;] . &quot;&lt;/p&gt;&quot;;
//Or maybe it&#39;s closer to this?
echo &quot;&lt;p&gt;&quot; . $rsvpData[$v][&quot;name&quot;] . &quot; - &quot; . $rsvpData[$v][&quot;rsvp_status&quot;] . &quot;&lt;/p&gt;&quot;;
}
array_push($alreadyProcessed, $v[&quot;group_id&quot;]);
} else {
//Already done, can skip.
};
}

The array $rsvpData

Array
(
[1] =&gt; Array
(
[group_id] =&gt; 1
[name_id] =&gt; 1
[invite_name] =&gt; Billy Joel
[name] =&gt; Billy Joel
[rsvp_status] =&gt; 1
)
[2] =&gt; Array
(
[group_id] =&gt; 2
[name_id] =&gt; 2
[invite_name] =&gt; Doe Family
[name] =&gt; John Doe
[rsvp_status] =&gt; 1
)
[3] =&gt; Array
(
[group_id] =&gt; 2
[name_id] =&gt; 3
[invite_name] =&gt; Doe Family
[name] =&gt; Jane Doe
[rsvp_status] =&gt; 0
)
[4] =&gt; Array
(
[group_id] =&gt; 3
[name_id] =&gt; 4
[invite_name] =&gt; Clark Kent
[name] =&gt; Clark Kent
[rsvp_status] =&gt; 0
)
[5] =&gt; Array
(
[group_id] =&gt; 4
[name_id] =&gt; 5
[invite_name] =&gt; David Smith
[name] =&gt; David Smith
[rsvp_status] =&gt; 2
)
[6] =&gt; Array
(
[group_id] =&gt; 5
[name_id] =&gt; 6
[invite_name] =&gt; Chris &amp; Julie
[name] =&gt; Chris
[rsvp_status] =&gt; 1
)
[8] =&gt; Array
(
[group_id] =&gt; 5
[name_id] =&gt; 6
[invite_name] =&gt; Chris &amp; Julie
[name] =&gt; Julie
[rsvp_status] =&gt; 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"]

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:

确定