Created
July 26, 2018 14:47
-
-
Save yano3nora/26d95c1f3841c0820630cd93d8f7f11b to your computer and use it in GitHub Desktop.
[php: <=>] Spaceship operator on PHP 7. #php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Spaceship operator. | |
* @link https://qiita.com/Hiraku/items/62821d7d0e7af1ac211e | |
* @link https://teratail.com/questions/61222#reply-97259 | |
*/ | |
// 多次元配列の Diff | |
$arr1 = [ | |
['flag' => 2, 'status' => 25], | |
['flag' => 64, 'status' => 25], | |
]; | |
$arr2 = [ | |
['flag' => 122, 'status' => 25], | |
['flag' => 2, 'status' => 25], | |
['flag' => 88, 'status' => 25], | |
]; | |
$compare = function ($x, $y) { | |
return $x['flag'] <=> $y['flag'] ?: $x['status'] <=> $y['status']; | |
}; | |
var_dump(array_merge( | |
array_udiff($arr2, $arr1, $compare), | |
array_udiff($arr1, $arr2, $compare) | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment