PHP - 按模式排序数组

时间:2015-02-11 10:51:26

标签: php arrays sorting

我有一个字符串数组

[ "one", "Two", "FOUR", "Four", "three", "two"] 

我需要创建一个如下所示的字符串:

"一,三,二

一,二,三,四,二

一,二,四,三,四,二"。

我将不胜感激任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

短数组语法需要PHP> = 5.4

$master = [ "one", "Two", "FOUR", "Four", "three", "two"];

// Filter $master into arrays containing the appropriate lower, ucfirst and upper values
$lowerSet = array_filter(
    $master,
    function($value) {
        return strtolower($value) == $value;
    }
);

$ucfirstSet = array_filter(
    $master,
    function($value) {
        return ucfirst(strtolower($value)) == $value;
    }
);

$upperSet = array_filter(
    $master,
    function($value) {
        return strtoupper($value) == $value;
    }
);

function sorting(array $series) {
    $mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
    foreach($series as $valueSet) {
        $mi->attachIterator(new ArrayIterator($valueSet));
    }
    $line = [];
    foreach($mi as $values) {
        $line = array_merge($line, $values);
    }
    return array_filter($line);
}

$lines = [
    implode(', ', $lowerSet),
    implode(', ', sorting([$lowerSet, $ucfirstSet])),
    implode(', ', sorting([$lowerSet, $ucfirstSet, $upperSet]))
];
echo implode(PHP_EOL, $lines);

但如果[]替换为array()

,则可以使用PHP> = 5.3