jazzman1 Posted November 13, 2012 Share Posted November 13, 2012 Hi friends, assuming that I currently have an array like this, Array ( [0] => Array ( [[email protected]] => jazzman ) [1] => Array ( [[email protected]] => slippers ) [2] => Array ( [[email protected]] => jazz ) [3] => Array ( [[email protected]] => jazzman ) [4] => Array ( [[email protected]] => jazzman ) [5] => Array ( [[email protected]] => jazzman ) [6] => Array ( [[email protected]] => jazzman ) [7] => Array ( [[email protected]] => jazzman ) [8] => Array ( [[email protected]] => jazzman ) [9] => Array ( [[email protected]] => jazzman ) [10] => Array ( [[email protected]] => jazzman ) [11] => Array ( [[email protected]] => jazzman ) [12] => Array ( [[email protected]] => jazzman ) [13] => Array ( [[email protected]] => jazzman ) ) What is the best way resorting it and the final result I wanna be exactly like this: Array ( [0] => Array ( [[email protected]] => jazzman [[email protected]] => slippers [[email protected]] => jazz ) [1] => Array ( [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman ) [2] => Array ( [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman ) [3] => Array ( [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman ) [4] => Array ( [[email protected]] => jazzman [[email protected]] => jazzman ) ) Thank you in advance! jazz. Link to comment https://forums.phpfreaks.com/topic/270644-sorting-array/ Share on other sites More sharing options...
requinix Posted November 13, 2012 Share Posted November 13, 2012 Move everything into a one-dimensional array then array_chunk it. Link to comment https://forums.phpfreaks.com/topic/270644-sorting-array/#findComment-1392115 Share on other sites More sharing options...
jazzman1 Posted November 13, 2012 Author Share Posted November 13, 2012 But, it's alredy a dimensional array. If I'm using the code like below, see the result: $query = 'SELECT * FROM users'; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $menu_array[$row['id']] = array($row['email']=>$row['name']); } function partition( $list, $p ) { $listlen = count( $list); $partlen = floor( $listlen / $p ); $partrem = $listlen % $p; $partition = array(); $mark = 0; for ($px = 0; $px < $p; $px++) { $incr = ($px < $partrem) ? $partlen + 1 : $partlen; $partition[$px]= array_slice($list, $mark, $incr); $mark += $incr; } return $partition; } echo '<pre>'.print_r( partition( $menu_array, 5 ), 1).'</pre>'; Results: Array ( [0] => Array ( [0] => Array ( [[email protected]] => jazzman ) [1] => Array ( [[email protected]] => slippers ) [2] => Array ( [[email protected]] => tuparov ) ) [1] => Array ( [0] => Array ( [[email protected]] => jazzman ) [1] => Array ( [[email protected]] => jazzman ) [2] => Array ( [[email protected]] => jazzman ) ) [2] => Array ( [0] => Array ( [[email protected]] => jazzman ) [1] => Array ( [[email protected]] => jazzman ) [2] => Array ( [[email protected]] => jazzman ) ) [3] => Array ( [0] => Array ( [[email protected]] => jazzman ) [1] => Array ( [[email protected]] => jazzman ) [2] => Array ( [[email protected]] => jazzman ) ) [4] => Array ( [0] => Array ( [[email protected]] => jazzman ) [1] => Array ( [[email protected]] => jazzman ) ) ) Link to comment https://forums.phpfreaks.com/topic/270644-sorting-array/#findComment-1392123 Share on other sites More sharing options...
Jessica Posted November 13, 2012 Share Posted November 13, 2012 Move everything into a one-dimensional array then array_chunk it. Your array is currently not ONE dimensional. It would be easy to make it so. Link to comment https://forums.phpfreaks.com/topic/270644-sorting-array/#findComment-1392125 Share on other sites More sharing options...
Psycho Posted November 13, 2012 Share Posted November 13, 2012 NO, that is not a single-dimensional array. It is a multidimensional array array where each subarray has a single value. This is a single dimensional array Array ( [[email protected]] => jazzman [[email protected]] => slippers [[email protected]] => jazz [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman [[email protected]] => jazzman ) The problem is how this array is being created while ($row = mysql_fetch_assoc($result)) { $menu_array[$row['id']] = array($row['email']=>$row['name']); } The array() after the equal sign is creating a subarray! It should just be this while ($row = mysql_fetch_assoc($result)) { $menu_array[$row['email']] = $row['name']; } To get the array into "chunks" of three you could implement logic in the creation process above, or just use array_chunk() after the single dimensional array is created while ($row = mysql_fetch_assoc($result)) { $menu_array[$row['id']] = array($row['email']=>$row['name']); } $menu_array = array_chunk($menu_array, 3, true); Link to comment https://forums.phpfreaks.com/topic/270644-sorting-array/#findComment-1392126 Share on other sites More sharing options...
jazzman1 Posted November 13, 2012 Author Share Posted November 13, 2012 Wow..... I'm just a stupid man. Thank you Psycho Thanks everyone for your help. Solved! Link to comment https://forums.phpfreaks.com/topic/270644-sorting-array/#findComment-1392131 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.