From: <gem...@li...> - 2012-09-11 14:01:07
|
Revision: 938 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=938&view=rev Author: matijsdejong Date: 2012-09-11 14:00:57 +0000 (Tue, 11 Sep 2012) Log Message: ----------- Added keySplit function to MUtil_Ra Modified Paths: -------------- trunk/library/classes/MUtil/Ra.php trunk/test/classes/MUtil/RaTest.php Property Changed: ---------------- trunk/scripts/ Modified: trunk/library/classes/MUtil/Ra.php =================================================================== --- trunk/library/classes/MUtil/Ra.php 2012-09-11 13:10:36 UTC (rev 937) +++ trunk/library/classes/MUtil/Ra.php 2012-09-11 14:00:57 UTC (rev 938) @@ -470,6 +470,31 @@ } /** + * This functions splits an array into two arrays, one containing + * the integer keys and one containing the string keys and returns + * an array containing first the integer key array and then the + * string key array. + * + * @param array $arg The input array + * @return array array(integer_keys, string_keys) + */ + public static function keySplit(array $arg) + { + $nums = array(); + $strings = array(); + + foreach ($arg as $key => $value) { + if (is_integer($key)) { + $nums[$key] = $value; + } else { + $strings[$key] = $value; + } + } + + return array($nums, $strings); + } + + /** * A function that transforms an array in the form key1, value1, key2, value2 into array(key1 => value1, key2 => value2). * * When the $args array contains only a single sub array, then this value is assumed to be the return value. This allows Property changes on: trunk/scripts ___________________________________________________________________ Modified: svn:ignore - docs gems.lint reports tmp + depend-chart_map.shtml depend-overview_map.shtml docs gems.lint reports tmp Modified: trunk/test/classes/MUtil/RaTest.php =================================================================== --- trunk/test/classes/MUtil/RaTest.php 2012-09-11 13:10:36 UTC (rev 937) +++ trunk/test/classes/MUtil/RaTest.php 2012-09-11 14:00:57 UTC (rev 938) @@ -76,4 +76,36 @@ $args = MUtil_Ra::args(array(0 => array(0 => 'f', 1 => array('o' => '0', 0 => 'b')), 1 => array('a' => array('r' => 'r')))); $this->assertEquals($args, array(0 => 'f', 'o' => '0', 1 => 'b', 'a' => array('r' => 'r'))); } + + public function testKeySplit() + { + $args = array(0 => '0', 'a' => 'a', 1 => '1', 'b' => 'b', '2' => '2'); + list($nums, $strings) = MUtil_Ra::keySplit($args); + $this->assertEquals($nums, array(0 => '0', 1 => '1', '2' => '2')); + $this->assertEquals($strings, array('a' => 'a', 'b' => 'b')); + } + + public function testKeySplitNumOnly() + { + $args = array(0 => '0', 1 => '1', '2' => '2'); + list($nums, $strings) = MUtil_Ra::keySplit($args); + $this->assertEquals($nums, array(0 => '0', 1 => '1', '2' => '2')); + $this->assertEquals($strings, array()); + } + + public function testKeySplitStringOnly() + { + $args = array('a' => 'a', 'b' => 'b'); + list($nums, $strings) = MUtil_Ra::keySplit($args); + $this->assertEquals($nums, array()); + $this->assertEquals($strings, array('a' => 'a', 'b' => 'b')); + } + + public function testKeySplitEmpty() + { + $args = array(); + list($nums, $strings) = MUtil_Ra::keySplit($args); + $this->assertEquals($nums, array()); + $this->assertEquals($strings, array()); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |