我有两个多维数组,需要根据不同的键以与第二个数组相同的顺序对第一个数组进行排序(但它们的值是相同的).在下面的示例中,我需要将$allOptions
排序为与$regOptions
相同的顺序,但基于值clID == optID
.
但是,并不是所有的$allOptions
个子数组(ClID)都出现在$regOptions
个子数组(OptID)中,所以$allOptions
个子数组中任何不匹配的元素都会被抛到数组的底部/末尾.
我怎么能这样做呢?
$allOptions = array(
array("clID"=> 171, ...other values),
array("clID"=> 191, ...other values),
array("clID"=> 131, ...other values),
array("clID"=> 101, ...other values),
array("clID"=> 201, ...other values),
array("clID"=> 181, ...other values),
...
array("clID"=> 99, ...other values), // not in regOptions
array("clID"=> 129, ...other values) // not in regOptions
array("clID"=> 139, ...other values)
) ;
$regOptions = array(
array("order"=>1,"optID"=> 131, ...other values),
array("order"=>2,"optID"=> 191, ...other values),
array("order"=>3,"optID"=> 181, ...other values),
array("order"=>4,"optID"=> 139, ...other values),
array("order"=>5,"optID"=> 101, ...other values),
array("order"=>6,"optID"=> 201, ...other values),
array("order"=>7,"optID"=> 171, ...other values)
...
) ;
因此,输出将是:
$allOptions = array(
array("clID"=> 131, ...other values),
array("clID"=> 191, ...other values),
array("clID"=> 181, ...other values),
array("clID"=> 139, ...other values)
array("clID"=> 101, ...other values),
array("clID"=> 201, ...other values),
array("clID"=> 171, ...other values),
...
array("clID"=> 99, ...other values), // not in regOptions
array("clID"=> 129, ...other values) // not in regOptions
) ;