Php 关于数组_相交

Php 关于数组_相交,php,moodle,array-intersect,Php,Moodle,Array Intersect,我有两个数组。一个是来自Moodle($allUsers)的用户名,另一个是来自外部源($dataClip)的用户名。如果还没有注册,我需要比较并批量添加 function buildURL($year, $period, $typeperiod,$course) { return 'https://clip.unl.pt/sprs?lg=pt&year='.$year.'&uo=97747&srv=rsu&p='.$period.'&tp='.$type

我有两个数组。一个是来自Moodle($allUsers)的用户名,另一个是来自外部源($dataClip)的用户名。如果还没有注册,我需要比较并批量添加

function buildURL($year, $period, $typeperiod,$course)
{
return 'https://clip.unl.pt/sprs?lg=pt&year='.$year.'&uo=97747&srv=rsu&p='.$period.'&tp='.$typeperiod.'&md=3&rs='.$course.'&it=1030123459';
}

function doRequest_with_FileGetContents($url)
{
return file_get_contents($url);
}

function getallUsers(){
global $DB;
$allusers=array();
$users= $DB->get_records('user');
foreach($users as $user){
$allusers[]= $user->username."<br/>";

}
return $allusers;
}
function processXML($xmlContent){
$xmlObj= new SimpleXMLElement($xmlContent);
$result=array();
foreach($xmlObj->unidade_curricular->inscritos->aluno as $aluno){
$result[]= $aluno->identificador."<br/>";

}
return $result;
}

$allUsers= getallUsers();
$dataClip= processXML($content_b);
$courseid= required_param('id', PARAM_INT); 
$context= get_context_instance(CONTEXT_COURSE, $courseid);//Getting students who are already enrolled                                                     
$students= get_role_users(5,$context);

if(is_array($dataClip)){ //eliminates warnings of Invalid Argument supplied in foreach
foreach($dataClip as $newdata){
    $duplicate=false;
    if(is_array($allUsers)){
    foreach($allUsers as $dataMoodle){
        // if there is a match
        if($newdata==$dataMoodle){
            // if student is enrolled on moodle course page.
           if($students){
           $duplicate=true;
        continue;
            }
    else {
        $duplicate=false;
        $results=array_intersect((array)$newdata,(array)$dataMoodle); // complains about not being an array
        //print_r($results);
        echo implode('<br/>',$results);
}

else{
    $duplicate= false;
    continue;
    } 
}
}
}
}
函数构建URL($year、$period、$typeperiod、$course)
{
返回'https://clip.unl.pt/sprs?lg=pt&year=“.$year.&uo=97747&srv=rsu&p=”.$period.&tp=”.$typeperiod.&md=3&rs=”.$course.&it=1030123459';
}
函数doRequest_与_FileGetContents($url)
{
返回文件获取内容($url);
}
函数getallUsers(){
全球$DB;
$alluser=array();
$users=$DB->get_记录('user');
foreach($users作为$user){
$allusers[]=$user->username.“
”; } 返回$alluser; } 函数processXML($xmlContent){ $xmlObj=新的simplexmlement($xmlContent); $result=array(); foreach($xmlObj->unidade_课程->inscritos->aluno as$aluno){ $result[]=$aluno->identificator.“
”; } 返回$result; } $allUsers=getallUsers(); $dataClip=processXML($content_b); $courseid=所需参数('id',参数INT); $context=get_context_实例(context_课程,$courseid)//获取已经注册的学生 $students=获取角色用户(5$context); if(is_array($dataClip)){//消除foreach中提供的无效参数的警告 foreach($dataClip作为$newdata){ $duplicate=false; if(is_数组($alluser)){ foreach($dataMoodle){ //如果有比赛 如果($newdata==$dataMoodle){ //如果学生在moodle课程页面注册。 如果(学生){ $duplicate=true; 继续; } 否则{ $duplicate=false; $results=array_intersect((array)$newdata,(array)$dataMoodle);//抱怨不是数组 //打印(结果); 回波内爆(“
”,$results); } 否则{ $duplicate=false; 继续; } } } } }
数组_intersect给了我两个数组之间的通用用户名,但是当我将其中一个添加到我的课程页面时,我没有得到任何输出。因此,就像abc和ab之间的交集是[],而不是ab

编辑:dataCLIP有300多个名称,但其中有

a、 玛雅

a、 卡布拉尔

d、 马修斯

这是Moodle的所有用户

客人

管理员

xpto.xy

a、 玛雅

d、 马诺

a、 卡布拉尔

d、 马修斯


我的逻辑在哪里失败?

对于注册用户,您可能需要查看/lib/enrollib.php中的函数enrol\u user\u bulk(stdClass$instance,$userid,…)

像这样的

$plugin = enrol_get_plugin('manual');
$courses = ... // get the distinct course ids
foreach ($courses as $course) {
    $instance = $DB->get_record('enrol', array('courseid' => $courseid, 'enrol' => 'manual');
    $userids = ... // get userid's to enrol on this course
    $plugin->enrol_user_bulk($instance, $userids) 
}

你能解释一下你的代码吗:如果$allUsers和$dataClip是用户名数组(字符串,对吗?),那么你的foreach循环中$newdata和$dataMoodle中包含了什么?我想我不明白为什么你在开始foreach之前不比较数组。为什么不先找到并删除重复的(或者使用array_diff来找到唯一的)如果这给了你一个非空的数组进程,那么@S Korolev allUsers是一个函数,它可以让我获得所有Moodle用户。而dataClip是一个函数,它可以从url中获取名为Identificator的标记中的所有值。我也会发布这些方法和url。当你将所有内容都转换为标量并且已经知道它们不是重复的吗?为什么不$results[]=$newData;?您是否介意修正缩进?@Elin我需要确保两个数组之间存在重复项,因为我想将在Moodle上注册的学生添加到课程页面。如果该用户已经在该课程页面上注册,我将跳过他并转到下一个。如果intersect ABC,我希望看到ABC。但是如果A是已经报名参加课程,我想看BC,而不是[]。您好,/lib/enrollib.php中没有enrol_user_bulk方法。也许它被弃用了?我使用的是Moodle 2.5。或者您指的是enrol_用户函数?我有没有办法使用存储在数组中的用户名而不是用户名来注册它们?啊,刚刚意识到该函数是Totara而不是Moodle的一部分-有该函数的副本Moodle HQ github上的更新——Moodle的大部分工作都是基于ID的,因此您需要使用ID而不是用户名。好的,谢谢。接下来几天,您将深入研究并可能发表一篇新文章。在这里,您可能想看看Totara Sync——它使用csv文件或数据库表来同步用户在您提供的第一个totara链接的lib/enrollib.php中,通过enrol\u bulk\u用户方法的批处理看到函数调用insert\u records\u。这是从哪里来的?