← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1768: [core] Implement array_group() function

 

------------------------------------------------------------
revno: 1768
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Sat 2010-03-06 14:56:49 +0100
message:
  [core] Implement array_group() function
  
  The array_group() function groups multiple arrays based on
  the value of the specified key.
  
  See the docs for more information.
modified:
  core/array.lib.php
  core/array.test.php


--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'core/array.lib.php'
--- core/array.lib.php	2010-02-16 20:32:26 +0000
+++ core/array.lib.php	2010-03-06 13:56:49 +0000
@@ -675,4 +675,61 @@
 	return uksort($arr, 'strnatcasecmp');
 }
 
+
+/**
+ * Group multiple arrays based on the value of the specified key.
+ *
+ * This function groups a set of arrays into an associative array index by name.
+ * Each group in the result contains the input arrays that share the same value
+ * for the specified key. In combination with sorting, e.g. using ksort(), this
+ * can be used to iterate over different dimensions of multidimensional data.
+ *
+ * Example:
+ *
+ * \code
+ * $arrays = array(
+ * 	array('name' => 'apple',  'howmany' => 12),
+ * 	array('name' => 'apple',  'howmany' => 10),
+ * 	array('name' => 'banana', 'howmany' => 12),
+ * 	array('name' => 'pear',   'howmany' =>  5),
+ * );
+ * $groups_by_name = array_group($arrays, 'name');
+ * $groups_by_howmany = array_group($arrays, 'howmany');
+ *
+ * // $groups_by_name now looks like this:
+ * //   apple:  [ (name: apple, howmany: 12),
+ * //             (name: apple, howmany: 10) ],
+ * //   banana: [ (name: banana, howmany: 12) ],
+ * //   pear:   [ (name: pear, howmany: 5) ]
+ * \endcode
+ *
+ * \param $arrays
+ *   An array of arrays
+ * \param $key
+ *   The key to use for grouping
+ *
+ * \return
+ *   Associative array containing the passed \c $arrays, indexed by value
+ */
+function array_group($arrays, $key)
+{
+	assert('is_numeric_array($arrays);');
+	$groups = array();
+
+	foreach ($arrays as $arr)
+	{
+		assert('is_array($arr); // only arrays can be grouped');
+		assert('array_key_exists($key, $arr); // key must be set in all arrays');
+
+		$value = to_string($arr[$key]);
+
+		if (!array_key_exists($value, $groups))
+			$groups[$value] = array();
+
+		$groups[$value][] = $arr;
+	}
+
+	return $groups;
+}
+
 ?>

=== modified file 'core/array.test.php'
--- core/array.test.php	2010-02-16 20:28:22 +0000
+++ core/array.test.php	2010-03-06 13:56:49 +0000
@@ -204,6 +204,37 @@
 		natksort($data);
 		$this->assertEquals($data, $expected);
 	}
+
+	/**
+	 * Test grouping
+	 */
+	function test_array_group()
+	{
+		$arrays = array(
+			array('name' => 'apple',  'howmany' => 12),
+			array('name' => 'apple',  'howmany' => 10),
+			array('name' => 'banana', 'howmany' => 12),
+			array('name' => 'pear',   'howmany' => 5),
+		);
+
+		$groups_by_name = array_group($arrays, 'name');
+		$this->assertEquals(3, count($groups_by_name));
+		$this->assertArrayHasKey('apple', $groups_by_name);
+		$this->assertArrayHasKey('banana', $groups_by_name);
+		$this->assertArrayHasKey('pear', $groups_by_name);
+		$this->assertEquals(2, count($groups_by_name['apple']));
+		$this->assertEquals(1, count($groups_by_name['banana']));
+		$this->assertEquals(1, count($groups_by_name['pear']));
+
+		$groups_by_howmany = array_group($arrays, 'howmany');
+		$this->assertEquals(3, count($groups_by_howmany));
+		$this->assertArrayHasKey(5, $groups_by_howmany);
+		$this->assertArrayHasKey(10, $groups_by_howmany);
+		$this->assertArrayHasKey(12, $groups_by_howmany);
+		$this->assertEquals(1, count($groups_by_howmany[5]));
+		$this->assertEquals(1, count($groups_by_howmany[10]));
+		$this->assertEquals(2, count($groups_by_howmany[12]));
+	}
 }
 
 ?>