[PHP-DEV] Bug #11077 Updated: two new array functions From: venaas <email protected>
Date: 05/24/01

ID: 11077
Updated by: venaas
Reported By: sbergmann <email protected>
Status: Open
Bug Type: Feature/Change Request
Operating system:
PHP Version: 4.0.5
Assigned To:
Comments:

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 )

Previous Comments:
---------------------------------------------------------------------------

[2001-05-24 04:27:57] sbergmann <email protected>
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.

---------------------------------------------------------------------------

ATTENTION! Do NOT reply to this email!
To reply, use the web interface found at http://bugs.php.net/?id=11077&edit=2

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>