Jump to content

Sorting Array


jazzman1

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.