在PHP小排序算法 [英] Small sorting algorithm in php

查看:96
本文介绍了在PHP小排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单的算法,但我就是无法避开它。

It should quite simple algorithm, but I just can't get around it.

我有一些阵列按字母顺序排列

I have some arrays in alphabetical order

[0] => Array
    (
        [0] => a
        [1] => b
        [2] => c
    )

和例如:

[0] => Array
    (
        [0] => a
        [1] => b
        [2] => c
        [3] => d
    )

和我需要将它们整理成行。例如:

and I need to sort them into rows. For example:

我应该得到表3列和尽可能多的行,因为它可能会得到它应该是按字母顺序排列。

I should receive a table with 3 columns and as many rows as it may get and it should be in alphabetical order.

下面是一个例子: 第一阵列应该被转换为

Here is an example: First array should be converted into

[0] => Array
    (
        [0] => Array
            (
                [0] => a
                [1] => b
                [2] => c
            )

    )

但第二个应该是因为

But second one should be as

[1] => Array
    (
        [0] => Array
            (
                [0] => a
                [1] => c
                [2] => d
            )
        [1] => Array
            (
                [0] => b
            )
    )

我写它 PHP ,因此,如果有人可以帮助我将非常AP preciated。

I'm writing it in php, so if anyone can help I would be really appreciated.

UPD: code例如:

function sortAsOrdered( array $categories )
{
    foreach ( $categories as $groupId => $group )
    {
        $regroupMenuItems = array();
        $limit            = count( $group );
        $rows             = ceil( $limit / 3 );

        for ( $i = 0; $i < $rows; ++$i )
        {
            $jumper = 0;

            for ( $j = 0; $j < 3; $j++ )
            {
                if ( 0 == $jumper )
                {
                    $jumper = $i;
                }

                if ( isset( $group[ $jumper ] ) )
                {
                    $regroupMenuItems[ $i ][ $j ] = $group[ $jumper ];
                }

                $jumper = $jumper + $rows;
            }
        }

        $categories[ $groupId ] = $regroupMenuItems;
    }

    return $categories;
}

大家好我解决了这一之一。在这里,你可以看到我的算法 http://pastebin.com/xe2yjhYW 。 但是,不要难过你的帮助不会白费。我可能将会把奖金只为那些谁帮助这一难治算法适合我。

Guys I solved this one. Here you could see my algorithm http://pastebin.com/xe2yjhYW. But don't be sad your help will not go in vain. I probably will place bounty just for those who helped with this dificult algorithm for me.

专家感谢一个更多的时间。你的思想启发了我有不同的想法。

Guys thanks one more time. Your thoughts inspired me to think differently.

推荐答案

如果不久说出来,那么这里是该算法的方法。

If to say it shortly, then here is a method for that algorithm.

/**
 * @param array $toTransform
 * @param int $columnsMax
 * @return array
 */
private function transformation( array $toTransform, $columnsMax = 3 )
{
    // First divide array as you need
    $listlen   = count( $toTransform );
    $partlen   = floor( $listlen / $columnsMax );
    $partrem   = $listlen % $columnsMax;
    $partition = array();
    $mark      = 0;

    for ( $px = 0; $px < $columnsMax; $px++ )
    {
        $incr             = ( $px < $partrem ) ? $partlen + 1 : $partlen;
        $partition[ $px ] = array_slice( $toTransform, $mark, $incr );
        $mark             += $incr;
    }

    // Secondly fill empty slots for easy template use
    $result = array();

    for ( $i = 0; $i < count( $partition[0] ); $i++ )
    {
        $tmp = array();

        foreach ( $partition as $column )
        {
            if ( isset( $column[ $i ] ) )
            {
                $tmp[] = $column[ $i ];
            }
            else
            {
                $tmp[] = '';
            }
        }

        $result[] = $tmp;
    }

    return $result;
}

此外,我包括PHPUnit的测试的。你可以找到它,这链接

这篇关于在PHP小排序算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆