#11077 [Opn]: two new array functions

From: Date: Sat, 31 May 2003 14:49:47 +0000
Subject: #11077 [Opn]: two new array functions
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969354131@lists.php.net to get a copy of this message
ID: 11077 Updated by: andrey@php.net Reported By: sbergmann@php.net Status: Open -Bug Type: Feature/Change Request +Bug Type: Documentation problem Operating System: any PHP Version: 4.0.5 Assigned To: andrei New Comment: Using array_splice both functions can be simulated. Your first example about array_insert() can be translated to <?php $input = array(1,2,3,5); array_splice ($input, 3, 0, 4); print_r($input); ?> Since array_splice() reindex the hash the desired behaviour is succeeded. del() is performed with : <?php $input = array(1,2,3,4,5,6); $a = array_splice ($input, 2, 1); print_r($input); ?> Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 6 ) -=-=-=-=- Switching to "documentation problem", maybe it's good to add these two examples to the documentation of array_splice. If not - just close. Previous Comments: ------------------------------------------------------------------------ [2001-05-24 05:28:52] sbergmann@php.net Sounds good to me. More important is the del() operation, though. ------------------------------------------------------------------------ [2001-05-24 04:46:37] venaas@php.net How about insert rather than double? It's more generic and double is simple if you have insert. Like this: <?php $test = array(1,2,3,5); array_insert($test, 3, 4); print_r($test); ?> Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) ------------------------------------------------------------------------ [2001-05-24 04:27:57] sbergmann@php.net 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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=11077&edit=1

« previous php.doc (#969354131) next »