note 44204 added to function.array-splice
| From: | gideon at i6developments dot com | Date: | Sun, 18 Jul 2004 01:45:23 +0000 |
| Subject: | note 44204 added to function.array-splice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-73465@lists.php.net to get a copy of this message | ||
array_splice dynamically updates the total number of entries into the array. So for instance I had a
case where I needed to insert a value into every 4th entry of the array from the back. The problem
was when it added the first, because the total number was dynamically updated, it would only add
after the 3rd then the 2nd and so one. The solution I found is to track the number of inserts which
were done and account for them dynamically.
Code:
<?php
$modarray = array_reverse($mili);
$trig=1;
foreach($modarray as $rubber => $glue) {
if($rubber!="<BR>") {
$i++;
$b++;
if ($i==4) {
$trig++;
if($trig<=2) {
array_splice($modarray,$b,0,"<BR>");
}elseif($trig>=3){
array_splice($modarray,$b+($trig-2),0,"<BR>");
}
$i=0;
};
};
};
$fixarray = array_reverse($modarray);
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-splice.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44204
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44204&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44204&report=yes
Search -- http://master.php.net/manage/user-notes.php