doujian4752 2019-06-28 10:14
浏览 67

来自mysql查询的唯一数组

Unfortunately I have one of those moments when my mind just feels frozen. Therefore I really need some hints on what am I doing wrong:

I'm getting multiple rows based on a category_id with:

$certificates= $DB->get_records('prog', array('category'=>$_POST['something']),'', $fields='*');

this returns:

Array
(
    [1] => stdClass Object
        (
            [id] => 1
            [category] => 1
            [sortorder] => 0
            [fullname] => First Aid Kit Certification
            [shortname] => CP101
            [idnumber] => 
            [summary] => 
            [endnote] => 
            [visible] => 1
            [availablefrom] => 0
            [availableuntil] => 0
            [available] => 1
            [timecreated] => 1561637136
            [timemodified] => 1561637944
            [usermodified] => 2
            [icon] => 
            [exceptionssent] => 0
            [audiencevisible] => 2
            [certifid] => 1
            [assignmentsdeferred] => 0
            [allowextensionrequests] => 1
        )

    [2] => stdClass Object
        (
            [id] => 2
            [category] => 1
            [sortorder] => 1
            [fullname] => Program fullname 101
            [shortname] => P101
            [idnumber] => 
            [summary] => 
            [endnote] => 
            [visible] => 1
            [availablefrom] => 0
            [availableuntil] => 0
            [available] => 1
            [timecreated] => 1561645727
            [timemodified] => 1561645727
            [usermodified] => 2
            [icon] => 
            [exceptionssent] => 0
            [audiencevisible] => 2
            [certifid] => 
            [assignmentsdeferred] => 0
            [allowextensionrequests] => 1
        )

    [3] => stdClass Object
        (
            [id] => 3
            [category] => 1
            [sortorder] => 2
            [fullname] => Certification program fullname 101
            [shortname] => CP101
            [idnumber] => S02
            [summary] => 
            [endnote] => 
            [visible] => 1
            [availablefrom] => 0
            [availableuntil] => 0
            [available] => 1
            [timecreated] => 1561707524
            [timemodified] => 1561707584
            [usermodified] => 2
            [icon] => 
            [exceptionssent] => 1
            [audiencevisible] => 2
            [certifid] => 2
            [assignmentsdeferred] => 0
            [allowextensionrequests] => 1
        )

)

Using this I get all the users that own this certificate with this(in a foreach):

$usrwcert = $DB->get_records('certif_completion', array('certifid' => $cert->certifid), '', $fields = '*');

and then I just do another foreach on this usrwcert and try and make a list of users with the results of it.

The thing is that each user has two certificates, therefore I get my users twice in the list and thats not something I want.

The print_r of usrwcert returns:

Array
(
    [2] => stdClass Object
        (
            [id] => 2
            [certifid] => 1
            [userid] => 2
            [certifpath] => 3
            [status] => 4
            [renewalstatus] => 0
            [timewindowopens] => 1590665879
            [timeexpires] => 1593344279
            [timecompleted] => 1561637992
            [timemodified] => 1561661794
        )

    [1] => stdClass Object
        (
            [id] => 1
            [certifid] => 1
            [userid] => 3
            [certifpath] => 2
            [status] => 3
            [renewalstatus] => 0
            [timewindowopens] => 1590666730
            [timeexpires] => 1593345130
            [timecompleted] => 1561665963
            [timemodified] => 1561709174
        )

)
Array
(
)
Array
(
    [3] => stdClass Object
        (
            [id] => 3
            [certifid] => 2
            [userid] => 2
            [certifpath] => 2
            [status] => 3
            [renewalstatus] => 0
            [timewindowopens] => 1590651581
            [timeexpires] => 1593329981
            [timecompleted] => 1561707581
            [timemodified] => 1561707582
        )

    [4] => stdClass Object
        (
            [id] => 4
            [certifid] => 2
            [userid] => 3
            [certifpath] => 2
            [status] => 3
            [renewalstatus] => 0
            [timewindowopens] => 1590651583
            [timeexpires] => 1593329983
            [timecompleted] => 1561707583
            [timemodified] => 1561707584
        )

)

How can I return only the unique fields in the last foreach? Here is my full code:

$certificates= $DB->get_records('prog', array('category'=>$_POST['something']),'', $fields='*');
foreach($certificates as $cert) {

            $usrwcert = $DB->get_records('certif_completion', array('certifid' => $cert->certifid), '', $fields = '*');

                foreach($usrwcert as $user){
                $userdet = $DB->get_record('user', array('id' => $user->userid), $fields = '*');
                 $mform->addElement('html', '<option value="'.$userdet->id.'">'.$userdet->firstname.' '.$userdet->lastname.' ('.$userdet->username.', '.$userdet->email.')</option>');

            }
        }

Thanks!

  • 写回答

2条回答 默认 最新

  • dtdr84101 2019-06-28 10:23
    关注

    You can use the break to execute the last foreach only once, in this way you can avoid the next record for the same user

    foreach($usrwcert as $user){
        $userdet = $DB->get_record('user', array('id' => $user->userid), $fields = '*');
        $mform->addElement('html', '<option value="'.$userdet->id.'">'.$userdet->firstname.' '.$userdet->lastname.' ('.$userdet->username.', '.$userdet->email.')</option>');
        break;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分