<?php
// 支付列表数组
$paymentList = [
['name' => '支付宝', 'info' => '支持多种支付场景', 'weight' => 3],
['name' => '微信支付', 'info' => '便捷的移动支付', 'weight' => 2],
['name' => '合作方A', 'info' => '我们的合作伙伴', 'weight' => 1],
['name' => '银联支付', 'info' => '安全可靠的支付方式', 'weight' => 4],
['name' => 'Apple Pay', 'info' => '苹果用户的专属支付', 'weight' => 5]
];
// 自定义排序函数
usort($paymentList, function($a, $b) {
// 根据权重排序
if ($a['weight'] == $b['weight']) {
// 如果权重相同,则按名称进行自然排序
return strcmp($a['name'], $b['name']);
}
return $a['weight'] <=> $b['weight']; //正序
return $b['weight'] <=> $a['weight']; //倒序
});
// 输出排序后的支付列表
print_r($paymentList);
标签:info,排序,自定义,weight,支付,php,name
From: https://www.cnblogs.com/qcy-blog/p/18675007