jazzman1 Posted November 13, 2012 Share Posted November 13, 2012 Hi friends, assuming that I currently have an array like this, Array ( [0] => Array ( [jazzman_1@gmail.com] => jazzman ) [1] => Array ( [slippers@gmail.com] => slippers ) [2] => Array ( [jazz@gmail.com] => jazz ) [3] => Array ( [jazzman_2@gmail.com] => jazzman ) [4] => Array ( [jazzman_3@gmail.com] => jazzman ) [5] => Array ( [jazzman_4@gmail.com] => jazzman ) [6] => Array ( [jazzman_5@gmail.com] => jazzman ) [7] => Array ( [jazzman_6@gmail.com] => jazzman ) [8] => Array ( [jazzman_7@gmail.com] => jazzman ) [9] => Array ( [jazzman_8@gmail.com] => jazzman ) [10] => Array ( [jazzman_9@gmail.com] => jazzman ) [11] => Array ( [jazzman_10@gmail.com] => jazzman ) [12] => Array ( [jazzman_11@gmail.com] => jazzman ) [13] => Array ( [jazzman_12@gmail.com] => jazzman ) ) What is the best way resorting it and the final result I wanna be exactly like this: Array ( [0] => Array ( [jazzman_1@gmail.com] => jazzman [slippers@gmail.com] => slippers [jazz@gmail.com] => jazz ) [1] => Array ( [jazzman_2@gmail.com] => jazzman [jazzman_3@gmail.com] => jazzman [jazzman_4@gmail.com] => jazzman ) [2] => Array ( [jazzman_5@gmail.com] => jazzman [jazzman_6@gmail.com] => jazzman [jazzman_7@gmail.com] => jazzman ) [3] => Array ( [jazzman_8@gmail.com] => jazzman [jazzman_9@gmail.com] => jazzman [jazzman_10@gmail.com] => jazzman ) [4] => Array ( [jazzman_11@gmail.com] => jazzman [jazzman_12@gmail.com] => jazzman ) ) Thank you in advance! jazz. Quote 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. Quote 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 ( [jazzman_1@gmail.com] => jazzman ) [1] => Array ( [slippers@gmail.com] => slippers ) [2] => Array ( [tuparov@gmail.com] => tuparov ) ) [1] => Array ( [0] => Array ( [jazzman_2@gmail.com] => jazzman ) [1] => Array ( [jazzman_3@gmail.com] => jazzman ) [2] => Array ( [jazzman_3@gmail.com] => jazzman ) ) [2] => Array ( [0] => Array ( [jazzman_4@gmail.com] => jazzman ) [1] => Array ( [jazzman_5@gmail.com] => jazzman ) [2] => Array ( [jazzman_6@gmail.com] => jazzman ) ) [3] => Array ( [0] => Array ( [jazzman_7@gmail.com] => jazzman ) [1] => Array ( [jazzman_8@gmail.com] => jazzman ) [2] => Array ( [jazzman_9@gmail.com] => jazzman ) ) [4] => Array ( [0] => Array ( [jazzman_10@gmail.com] => jazzman ) [1] => Array ( [jazzman_9@gmail.com] => jazzman ) ) ) Quote 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. Quote 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 ( [jazzman_1@gmail.com] => jazzman [slippers@gmail.com] => slippers [jazz@gmail.com] => jazz [jazzman_2@gmail.com] => jazzman [jazzman_3@gmail.com] => jazzman [jazzman_4@gmail.com] => jazzman [jazzman_5@gmail.com] => jazzman [jazzman_6@gmail.com] => jazzman [jazzman_7@gmail.com] => jazzman [jazzman_8@gmail.com] => jazzman [jazzman_9@gmail.com] => jazzman [jazzman_10@gmail.com] => jazzman [jazzman_11@gmail.com] => jazzman [jazzman_12@gmail.com] => 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); Quote 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 (edited) Wow..... I'm just a stupid man. Thank you Psycho Thanks everyone for your help. Solved! Edited November 13, 2012 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/270644-sorting-array/#findComment-1392131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.