note 52075 added to ref.array
| From: | admin at snibworks dot com | Date: | Wed, 20 Apr 2005 15:40:24 +0000 |
| Subject: | note 52075 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88275@lists.php.net to get a copy of this message | ||
I have made a new array function called array_split.
It's like explode() for arrays. It searches the array for a specific value and splits the array
into chunks divided by that value.
<?php
function array_split($key, $array)
{
if(!is_array($array))trigger_error("Invalid type provided for
second parameter of array_split",E_USER_ERROR);
$rarray = array();
$curkey = 0;
foreach($array as $value)
{
if($value == $key)
{
$curkey++;
continue;
}
$rarray[$curkey][] = $value;
}
return $rarray;
}
//an example
$new_array = array('one','two','break','three',
'break','four','five','six');
var_dump(array_split('break',$new_array));
/*this returns:
array(3) {
[0]=>
array(2) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
}
[1]=>
array(1) {
[0]=>
string(5) "three"
}
[2]=>
array(3) {
[0]=>
string(4) "four"
[1]=>
string(4) "five"
[2]=>
string(3) "six"
}
}
*/
?>
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52075
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52075&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52075&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52075&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52075&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52075&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52075&report=yes
Search -- http://master.php.net/manage/user-notes.php