|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2001-05-24 04:46 UTC] venaas@php.net
[2001-05-24 05:28 UTC] sbergmann@php.net
[2003-05-31 09:49 UTC] andrey@php.net
[2003-07-15 02:09 UTC] philip@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 05:00:01 2025 UTC |
I'd like to propose two new array functions. I currently have the following implementation of del() and double() in PHP, but IMHO it would be better to have these (or atleast del()) natively in PHP. When using unset($array[$index]) on array, no re-indexing is performed: <?php $test = array(1,2,3,4,5,6); unset($test[2]); print_r($test); ?> Array ( [0] => 1 [1] => 2 [3] => 4 [4] => 5 [5] => 6 ) The del() method in the floowing code re-indexes the array: <?php class ftk_array { var $data; function ftk_array($data=array()) { $this->data=$data; } function del($pos) { for($i=$pos+1;$i<count($this->data);$i++) { $this->data[($i-1)]=$this->data[$i]; } unset($this->data[count($this->data)-1]); } function double($pos) { for($i=count($this->data);$i>$pos;$i--) { $this->data[($i)]=$this->data[$i-1]; } } } $test=new ftk_array(array(1,2,3,4,5,6)); $test->del(1); $test->double(4); print_r($test->data); ?> The double() method creates a clone of a given array element, while it maintainsthe indexes as well.