Skip to content

Instantly share code, notes, and snippets.

@lovecn
Created September 2, 2014 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovecn/29d9780abc1b59bfca13 to your computer and use it in GitHub Desktop.
Save lovecn/29d9780abc1b59bfca13 to your computer and use it in GitHub Desktop.
对二维数组按一维数组指定顺序排序
$mysort = new mysort;
$mysort->ceshi();
class mysort{
public function ceshi()
{
$arr1 = array(
array('id' => 1, 'name' => 'yhp'),
array('id' => 2, 'name' => 'yhp2'),
array('id' => 3, 'name' => 'yhp3'),
array('id' => 4, 'name' => 'yhp4'),
array('id' => 5, 'name' => 'yhp5'),
array('id' => 6, 'name' => 'yhp6'),
array('id' => 7, 'name' => 'yhp7'),
array('id' => 8, 'name' => 'yhp8'),
array('id' => 9, 'name' => 'yhp9'),
array('id' => 10,'name' => 'yhp10'),
);
echo "排序前的数组:<pre>";
print_r($arr1);
usort($arr1, array($this, "compare"));
echo "排序后的数组:";
print_r($arr1);
}
public function compare($a, $b)
{
$arr2 = array(6,8,7,9,3,2,4,1,5,10,11,12);
return (array_search($a['id'], $arr2) < array_search($b['id'], $arr2)) ? -1 : 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment