Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Created July 26, 2018 14:47
Show Gist options
  • Save yano3nora/26d95c1f3841c0820630cd93d8f7f11b to your computer and use it in GitHub Desktop.
Save yano3nora/26d95c1f3841c0820630cd93d8f7f11b to your computer and use it in GitHub Desktop.
[php: <=>] Spaceship operator on PHP 7. #php
/**
* 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