我有两个多维数组,需要根据不同的键以与第二个数组相同的顺序对第一个数组进行排序(但它们的值是相同的).在下面的示例中,我需要将$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
) ;

推荐答案

早些时候发布的所有答案都太费力了.使用array_column()生成查找array.使用usort()array_multisort()按该优先级数组排序.

代码:(Demo)

$priority = array_column($regOptions, 'order', 'optID');
usort(
    $allOptions,
    fn($a, $b) => ($priority[$a['clID']] ?? PHP_INT_MAX) <=> ($priority[$b['clID']] ?? PHP_INT_MAX)
);
var_export($allOptions);

如果您希望为所有未提及的clId值退回到按clID asc排序,那么使用Elvis操作符通过对列值进行简单的三向比较来打破这些限制.

$priority = array_column($regOptions, 'order', 'optID');
usort(
    $allOptions,
    fn($a, $b) =>
        ($priority[$a['clID']] ?? PHP_INT_MAX) <=> ($priority[$b['clID']] ?? PHP_INT_MAX)
        ?: $a['clID'] <=> $b['clID']
);

第一个脚本可以用array_multisort()按以下方式执行.请注意,这种方法可能比usort()执行得更好,因为它进行的查找更少,但它确实先对$priority排序,然后再对clId排序.(Demo)

$priority = array_column($regOptions, 'order', 'optID');
array_multisort(
    array_map(fn($v) => $priority[$v['clID']] ?? PHP_INT_MAX, $allOptions),
    $allOptions
);
var_export($allOptions);

Php相关问答推荐

Laravel迁移返回SQL错误,但SQL有效且工作正常

以编程方式更新现有Yith WooCommerce Booking的状态

从含有特殊字符的ANSI文本文件中读取数据是在移值,为什么?

根据WooCommerce中的客户计费国家/地区更改产品价格

为什么只有最后一次点击的点赞/心形会按预期改变 colored颜色 ,而其他的保持正常 colored颜色 ?

服务器迁移后无法上载文件-可能存在权限问题

在WooCommerce中添加特定产品类型的百分比费用和固定费用

Woocommerce 临时购物车税未根据第一个请求正确计算

我不明白为什么我收到未定义的数组键异常错误 - Laravel 9 PHP 8

如何处理 Null 上的 array_shift() ?

WooCommerce 按产品运输插件:仅收取较高的运输费用

在 mysql 上的 PDO 中启用自动提交并将自动提交设置为关闭

在 WooCommerce 我的帐户自定义部分显示未购买的产品

防止使用woocommerce_checkout_process下单

PHP文件如何从CSS执行

WooCommerce - 调用功能ID不正确.不应直接访问订单属性

我怎样才能在 .htaccess 中有 2 个参数?

从 laravel 中的日期类型中删除时间

转发和非转发呼叫 - 后期静态绑定

Laravel 响应 html 默认页面忽略控制器