douliao8402 2011-01-03 22:28
浏览 29
已采纳

如何从两个数组创建对?

        $q = "SELECT user_id FROM users WHERE partner='$qpartner' AND group_id='$gi' AND status='1'";
        $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));
            while ($row = mysqli_fetch_array($r)) {
                $usinneed[] = $row[0];
            }
        $affected = mysqli_affected_rows($dbc);
        $q2 = "SELECT user_id FROM users WHERE partner<>'$qpartner' AND group_id='$gi' AND status='1' ORDER BY RAND() LIMIT $affected";
        $r2 = mysqli_query ($dbc, $q2) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));
            while ($row = mysqli_fetch_array($r2)) {
                $potent_partners[] = $row[0];
            }

            $current = end($usinneed);
            $partners = array();
            foreach ($usinneed as $user)
            {
                $partners[$user] = $current;
                $current = $user;
                $q = "UPDATE users SET partner = '{$partners[$user]}' WHERE user_id = '{$user}'";
                mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));
            }

        print_r($partners);

I'm lost as soon as I come to the $current; that's where the changes are needed and I need help.
$usinneed is an array of all of the users who need a partner, and $potent_partners is an array of potential partners to pair with the Users In Need ($usinneed).
The arrays have an equal number of rows. How do I update the users table so that each $usinneed has their partner field set to one of the $potent_partners?

  • 写回答

3条回答 默认 最新

  • dongpo4197 2011-01-03 22:44
    关注

    Would array_combine be of any help perhaps?

    EDIT

    Given the comments below, here's the new details (formatted correctly):

    We start with the queries that populate two arrays: $usersInNeed (an array of those without partners) and $newPartners (an array of people that need to be paired up with $usersInNeed)

    We pair them up in PHP using array_combine. So

    $usersInNeed = Array(2,4); // Keys
    $newPartners = Array(8,5); // values
    $combined = array_combine($usersInNeed,$newPartners); // result: Array(2 => 8, 4 => 5);
    

    Now, we go back through the database and update those users with their partner. You can do this a bunch of ways, but a foreach seems simplest:

    // take each entry in $combined and break it out so the key is "$userInNeed" and the
    // value is "$newPartner" (illustrate what array_combine did)
    foreach ($combined as $userInNeed => $newPartner) 
    {
      $sql = "UPDATE users SET partner='{$newPartner}' WHERE user_id='{$userInNeed}';";
      // execute the statement
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看