note 55004 added to function.array-splice

From: Date: Fri, 22 Jul 2005 08:52:34 +0000
Subject: note 55004 added to function.array-splice
Groups: php.notes 
Request: Send a blank email to php-notes+get-92275@lists.php.net to get a copy of this message
To split an associative array based on it's keys, use this function: <?php function &array_split(&$in) { $keys = func_get_args(); array_shift($keys); $out = array(); foreach($keys as $key) { if(isset($in[$key])) $out[$key] = $in[$key]; else $out[$key] = null; unset($in[$key]); } return $out; } ?> Example: <?php $testin = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4); $testout =& array_split($testin, 'a', 'b', 'c'); print_r($testin); print_r($testout); ?> Will print: Array ( [d] => 4 ) Array ( [a] => 1 [b] => 2 [c] => 3 ) Hope this helps anyone! ---- Manual Page -- http://www.php.net/manual/en/function.array-splice.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55004 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55004&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55004&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55004&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55004&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55004&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55004&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55004&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92275) next »