Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I have some array in this property $this->categories and `dis` function is just nice way to print array.
- /*
- Here are my array examples
- */
- $categories = array(
- 1 => array(
- 'a', 'b', 'c',
- ),
- 2 => array(
- 'a', 'b', 'c', 'd', 'e', 'f', 'g',
- ),
- 3 => array(
- 'a', 'b', 'c', 'd', 'e', 'f',
- 'g', 'h', 'i', 'j', 'k', 'l',
- ),
- 4 => array(
- 'a', 'b',
- ),
- 5 => array(
- 'a',
- ),
- 6 => array(
- 'a', 'b', 'c', 'd', 'e',
- ),
- 7 => array(
- 'a', 'b', 'd', 'e', 'f',
- 'g', 'h', 'i', 'j', 'k', 'l',
- ),
- 8 => array(
- 'a', 'b', 'd', 'e', 'f',
- 'g', 'h', 'i', 'j', 'k', 'l',
- ),
- 9 => array(
- 'a', 'b',
- 'g', 'h',
- ),
- );
- function dividingArray( $array, $columnsMax = 3 )
- {
- dis( $array );
- $listlen = count( $array );
- $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( $array, $mark, $incr );
- $mark += $incr;
- }
- dis( $partition );
- $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;
- }
- dis( $result );
- }
- function dis( $var )
- {
- echo '<pre>';
- print_r( $var );
- echo '</pre>';
- }
- // Usage for testing
- echo '<h1>Test: 1</h1>';
- dividingArray( $categories[1] );
- echo '<h1>Test: 2</h1>';
- dividingArray( $categories[2] );
- echo '<h1>Test: 3</h1>';
- dividingArray( $categories[3] );
- echo '<h1>Test: 4</h1>';
- dividingArray( $categories[4] );
- echo '<h1>Test: 5</h1>';
- dividingArray( $categories[5] );
- echo '<h1>Test: 6</h1>';
- dividingArray( $categories[6] );
- echo '<h1>Test: 7</h1>';
- dividingArray( $categories[7] );
- echo '<h1>Test: 8</h1>';
- dividingArray( $categories[8] );
- echo '<h1>Test: 9</h1>';
- dividingArray( $categories[9] );
- echo '<h1>Test: 10</h1>';
- dividingArray( $categories[10] );
Add Comment
Please, Sign In to add comment