View difference between Paste ID: xe2yjhYW and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | // I have some array in this property $this->categories and `dis` function is just nice way to print array. |
2 | /* | |
3 | Here are my array examples | |
4 | */ | |
5 | ||
6 | $categories = array( | |
7 | 1 => array( | |
8 | 'a', 'b', 'c', | |
9 | ), | |
10 | 2 => array( | |
11 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
12 | ), | |
13 | 3 => array( | |
14 | 'a', 'b', 'c', 'd', 'e', 'f', | |
15 | 'g', 'h', 'i', 'j', 'k', 'l', | |
16 | ), | |
17 | 4 => array( | |
18 | 'a', 'b', | |
19 | ), | |
20 | 5 => array( | |
21 | 'a', | |
22 | ), | |
23 | 6 => array( | |
24 | 'a', 'b', 'c', 'd', 'e', | |
25 | ), | |
26 | 7 => array( | |
27 | 'a', 'b', 'd', 'e', 'f', | |
28 | 'g', 'h', 'i', 'j', 'k', 'l', | |
29 | ), | |
30 | 8 => array( | |
31 | 'a', 'b', 'd', 'e', 'f', | |
32 | 'g', 'h', 'i', 'j', 'k', 'l', | |
33 | ), | |
34 | 9 => array( | |
35 | 'a', 'b', | |
36 | 'g', 'h', | |
37 | ), | |
38 | ); | |
39 | ||
40 | function dividingArray( $array, $columnsMax = 3 ) | |
41 | { | |
42 | dis( $array ); | |
43 | ||
44 | $listlen = count( $array ); | |
45 | $partlen = floor( $listlen / $columnsMax ); | |
46 | $partrem = $listlen % $columnsMax; | |
47 | $partition = array(); | |
48 | $mark = 0; | |
49 | ||
50 | for ( $px = 0; $px < $columnsMax; $px++ ) | |
51 | { | |
52 | $incr = ( $px < $partrem ) ? $partlen + 1 : $partlen; | |
53 | $partition[ $px ] = array_slice( $array, $mark, $incr ); | |
54 | $mark += $incr; | |
55 | } | |
56 | ||
57 | dis( $partition ); | |
58 | ||
59 | $result = array(); | |
60 | ||
61 | for ( $i = 0; $i < count( $partition[0] ); $i++ ) | |
62 | { | |
63 | $tmp = array(); | |
64 | ||
65 | foreach ( $partition as $column ) | |
66 | { | |
67 | if ( isset( $column[ $i ] ) ) | |
68 | { | |
69 | $tmp[] = $column[ $i ]; | |
70 | } | |
71 | else | |
72 | { | |
73 | $tmp[] = ''; | |
74 | } | |
75 | } | |
76 | ||
77 | $result[] = $tmp; | |
78 | } | |
79 | ||
80 | dis( $result ); | |
81 | } | |
82 | ||
83 | function dis( $var ) | |
84 | { | |
85 | echo '<pre>'; | |
86 | print_r( $var ); | |
87 | echo '</pre>'; | |
88 | } | |
89 | ||
90 | // Usage for testing | |
91 | echo '<h1>Test: 1</h1>'; | |
92 | dividingArray( $categories[1] ); | |
93 | ||
94 | echo '<h1>Test: 2</h1>'; | |
95 | dividingArray( $categories[2] ); | |
96 | ||
97 | echo '<h1>Test: 3</h1>'; | |
98 | dividingArray( $categories[3] ); | |
99 | ||
100 | echo '<h1>Test: 4</h1>'; | |
101 | dividingArray( $categories[4] ); | |
102 | ||
103 | echo '<h1>Test: 5</h1>'; | |
104 | dividingArray( $categories[5] ); | |
105 | ||
106 | echo '<h1>Test: 6</h1>'; | |
107 | dividingArray( $categories[6] ); | |
108 | ||
109 | echo '<h1>Test: 7</h1>'; | |
110 | dividingArray( $categories[7] ); | |
111 | ||
112 | echo '<h1>Test: 8</h1>'; | |
113 | dividingArray( $categories[8] ); | |
114 | ||
115 | echo '<h1>Test: 9</h1>'; | |
116 | dividingArray( $categories[9] ); | |
117 | ||
118 | echo '<h1>Test: 10</h1>'; | |
119 | dividingArray( $categories[10] ); |