php array_diff无法处理值对象的集合

时间:2016-04-18 09:03:59

标签: php

        var_dump($this->externalProjects);
        var_dump($this->projects);
        $result = array_diff($this->externalProjects, $this->projects);
        print_r($result);

输出:

array (size=6) // external projects
  0 => 
    object(CloseCall\ValueObject\Project)[79]
      private 'name' => string '(A11) Tyre Pyrolysis Plant, Civil, Mech & Elec' (length=46)
  1 => 
    object(CloseCall\ValueObject\Project)[80]
      private 'name' => string '(B) - Millerhill New Depot Proposal' (length=35)
  2 => 
    object(CloseCall\ValueObject\Project)[81]
      private 'name' => string '(B13) Port of Tyne Wood Pellet Handling Facilities' (length=50)
  3 => 
    object(CloseCall\ValueObject\Project)[82]
      private 'name' => string '(C) - Millerhill Proposed New Depot' (length=35)
  4 => 
    object(CloseCall\ValueObject\Project)[83]
      private 'name' => string '(S) - EGIP New Depot Proposal' (length=29)
  5 => 
    object(CloseCall\ValueObject\Project)[85]
      private 'name' => string '00352 Allerton - Building fabric & structure survey' (length=51)
array (size=1) //projects
  0 => 
    object(CloseCall\ValueObject\Project)[15]
      private 'name' => string '(A11) Tyre Pyrolysis Plant, Civil, Mech & Elec' (length=46)

// no output for array_diff

1 个答案:

答案 0 :(得分:1)

要比较这样的对象,你应该看TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("images/pack.atlas")); mImage = atlas.createSprite("image")); mImage.setSize(mImage.getWidth() / 100 , mImage.getHeight() / 100); 。这两个函数之间的主要区别在于array_udiff将回调函数作为参数,然后用于比较对象。

请参阅文档。 http://php.net/manual/en/function.array-udiff.php

示例

array_udiff

此函数检查每个数组键值是否相同,如果是,则返回0,int comparison(Array $a, Array $b){ if ($a['id']==$b['id'] && $a['other']==$b['other'] && $a['timestamp']==$b['timestamp']){ return 0 }else{ return -1 } } array_udiff($a, $b, 'comparison') 将从比较数组中删除该对象。