gpt4 book ai didi

php - 根据段数对数组值进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:54:46 25 4
gpt4 key购买 nike

我有一个 URL 数组

例如:

['support/maintenance', 'support/', 'support/faq/laminator']

如何根据段数对其进行排序?

预期结果:

['support/faq/maninator', 'support/maintenance, 'support/']

最佳答案

使用 arsort 以相反的顺序组织,然后使用 usort 按字符串大小排序。

<?php

$arr = ['support/maintenance', 'support/', 'support/faq/laminator'];

function sortOrder($currentValue, $nextValue) {
$totalSegmentsOfTheCurrentIndex = count(preg_split('/\//', $currentValue, -1, PREG_SPLIT_NO_EMPTY));
$totalSegmentsOfTheNextIndex = count(preg_split('/\//', $nextValue, -1, PREG_SPLIT_NO_EMPTY));

// The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
return $totalSegmentsOfTheNextIndex - $totalSegmentsOfTheCurrentIndex;
}

usort($arr, 'sortOrder');

var_export($arr);

关于php - 根据段数对数组值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48017201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com