Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class NameObj {
- public $name;
- function __construct($name){
- $this->name = $name;
- }
- }
- class IdObj{
- public $id;
- function __construct($id){
- $this->id = $id;
- }
- }
- $idArray = array(
- new IdObj(1),
- new IdObj(2),
- new IdObj(3)
- );
- $nameArray = array(
- new NameObj('1 - Object 1 Name'),
- new NameObj('2 - Object 2 Name')
- );
- function custom_diff($oId, $oName){
- $splitName = explode(' - ', $oName->name);
- $idFromName = $splitName[0];
- $id = $oId->id;
- if($idFromName == $id) return 0;
- return $idFromName > $id ? 1 : -1;
- }
- $missing_objects = array_udiff($idArray, $nameArray, 'custom_diff');
- print_r($missing_objects);
- ?>
- PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
- PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
- PHP Notice: Undefined property: NameObj::$id in /home/ubuntu/test2.php on line 36
- PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
- PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
- Array
- (
- [1] => IdObj Object
- (
- [id] => 2
- )
- [2] => IdObj Object
- (
- [id] => 3
- )
- )
- $extractor = function($o) { return explode(' - ', $o->name)[0]; };
- $idsFromNames = array_flip(array_map($extractor, $nameArray));
- foreach ($idArray as $k => $o) {
- if (!isset($idsFromNames[$o->id])) {
- unset($idArray[$k]);
- }
- }
- function custom_diff($oId, $oName)
- {
- $one = $oId instanceof IdObj?$oId->id:explode(' - ', $oId->name)[0];
- $two = $oName instanceof NameObj?explode(' - ', $oName->name)[0]:$oName->id;
- return $one-$two;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement