Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Small sorting algorithm in 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
    )

and for example

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

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

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
            )
    )

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

UPD: Code example:

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;
}

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.

like image 1000
Eugene Avatar asked Feb 26 '26 06:02

Eugene


1 Answers

array_chunk() wold have been the solution but as you want it to be specially sorted, that wouldn't help you much.

So here is my five cents:

function array_chunk_vertical($input, $size_max) {
    $chunks = array();
    $chunk_count = ceil(count($input) / $size_max);

    $chunk_index = 0;
    foreach ($input as $key => $value) {
        $chunks[$chunk_index][$key] = $value;

        if (++$chunk_index == $chunk_count) {
            $chunk_index = 0;
        }
    }

    return $chunks;
}


$array = array('a', 'b', 'c', 'd', 'e', 'f');
var_dump(array_chunk_vertical($array, 2));

Which will give you:

array
  0 => 
    array
      0 => string 'a' (length=1)
      3 => string 'd' (length=1)
  1 => 
    array
      1 => string 'b' (length=1)
      4 => string 'e' (length=1)
  2 => 
    array
      2 => string 'c' (length=1)
      5 => string 'f' (length=1)

The downside of this function is that you can only tell the max number of elements in a chunk, and then it equally divides the array to chunks. So for [4] and max_size 3 you will get [2,2] unlike the expected [3,1].


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!