Jump to content

Sorting Array


jazzman1

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.