dongpaipu8394 2015-05-28 16:47
浏览 54

动态数组和Array_unintersect_assoc()|| Array_unintersect()

Having trouble trying to compare to dynamic arrays of objects.

Object:

class Skill
{
    public $SkillID;
    public $Profficiency;
    public $Required;
    public $Source;

    public function __construct($SkillID,$Prof,$Req,$Src){
        $this->SkillID = $SkillID;
        $this->Profficiency = $Prof;
        $this->Required = $Req;
        $this->Source = $Src;
    }
}

What I am doing is assigning 2 people their sets of skills. Person A & B can have any number of Skills. Skill::Source identifies that its from person A via a Boolean.

$SkillMatches = array_uintersect_assoc($this->Person_A->Skills,$this->Person_B->Skills,array($this, 'SkillIDComparison'));

private function SkillIDComparison($Skill_A,$Skill_B){
    print_r($Skill_A);
    echo '<br>';
    print_r($Skill_B);
    echo '<br>';
    echo '<br>';
    if ($Skill_A === $Skill_B) {
        return 0;
    } elseif ($Skill_A > $Skill_B) {
        return 1;
    } else {
        return -1;
    }
}

My issue is that if say Person_A has 5 Skills, and Person_B has 2 Skills, Array_uintersect_assoc() , does not check all 5 skills of Person_A

Skill Object ( [SkillID] => 6 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 
Skill Object ( [SkillID] => 3 [Profficiency] => 2 [Required] => 0 [Source] => ) 

Skill Object ( [SkillID] => 7 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 
Skill Object ( [SkillID] => 5 [Profficiency] => 2 [Required] => 1 [Source] => ) 

Now if I use Array_uintersect(), when the callable function is called it does not assign Person_A::Skills to $Skill_A and Person_B::Skills to $Skill_B respectfully, as well it seems to be random at which Skills are assigned to the parameters inside the Callable function.

$SkillMatches = array_uintersect($this->Person_A->Skills,$this->Person_B->Skills,array($this, 'SkillIDComparison'));

Skill Object ( [SkillID] => 7 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 
Skill Object ( [SkillID] => 6 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 

Skill Object ( [SkillID] => 8 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 
Skill Object ( [SkillID] => 7 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 

Skill Object ( [SkillID] => 5 [Profficiency] => 2 [Required] => 1 [Source] => ) 
Skill Object ( [SkillID] => 3 [Profficiency] => 2 [Required] => 0 [Source] => ) 

Skill Object ( [SkillID] => 6 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 
Skill Object ( [SkillID] => 3 [Profficiency] => 2 [Required] => 0 [Source] => ) 

Skill Object ( [SkillID] => 6 [Profficiency] => 1 [Required] => 0 [Source] => 1 ) 
Skill Object ( [SkillID] => 5 [Profficiency] => 2 [Required] => 1 [Source] => ) 

as you can see here half the time the function is called its not even comparing from the two different people. Only the last two calls actually compare Person_A and Person_B.

Any Assistance would be appreciated, Thanks.

  • 写回答

1条回答 默认 最新

  • doudou2121 2015-05-29 19:20
    关注

    You receive two objects in function SkillIDComparison. And if you want compare properties SkillID, so take them by $Skill_A->SkillID.

    So, change all next lines

    if ($Skill_A === $Skill_B) {
        return 0;
    } elseif ($Skill_A > $Skill_B) {
        return 1;
    } else {
        return -1;
    }
    

    by

    return $Skill_A->SkillID - $Skill_B->SkillID;
    

    Use array_uintersectbecause you want compare values but keys.

    I don't know whether this construction array($this, 'SkillIDComparison') is correct. But if use 3rd argument as 'SkillIDComparison' all works OK

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助