php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #11077 two new array functions
Submitted: 2001-05-24 04:27 UTC Modified: 2003-07-15 02:09 UTC
Votes:2
Avg. Score:2.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: sbergmann@php.net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.5 OS: any
Private report: No CVE-ID: None
 [2001-05-24 04:27 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-05-24 04:46 UTC] 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 05:28 UTC] sbergmann@php.net
Sounds good to me. More important is the del() operation, though.
 [2003-05-31 09:49 UTC] andrey@php.net
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.

 [2003-07-15 02:09 UTC] philip@php.net
Added an example to the array_splice() docs about inserting elements (the length parameter = 0).  An example to delete elements already exists there.  Also, added a see also array_splice() to the unset() docs.

Status->closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 15:01:29 2024 UTC