[php-src] Issue #11377: [SPL] Add some missed methods to `SplFixedArray`
| From: | alirezasalehizadeh | Date: | Mon, 05 Jun 2023 20:23:34 +0000 |
| Subject: | [php-src] Issue #11377: [SPL] Add some missed methods to `SplFixedArray` | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244605@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11377
Author: alirezasalehizadeh
### Description
### Description
It seems that some missing methods can be added to
SplFixedArray class
which can be useful:
1- Push or Append method:
By default for add a new element to SplFixedArray we can do this:
```php
// Initialize the array with a fixed length
$array = new SplFixedArray(3);
// Add a new element to array
$array[1] = 2;
var_dump($array[1]); // int(2)
```
We can have a missed push, add or append method like this:
```php
// Initialize the array with a fixed length
$array = new SplFixedArray(3);
// Add a new element to array
$array->push(1, 2);
var_dump($array[1]); // int(2)
```
2- each method:
Like array_map, this can be a easy way to work with array elements like this:
```php
// Initialize the array with a fixed length
$array = new SplFixedArray(2);
// Add new elements to array
$array[0] = 1;
$array[1] = 2;
// Applies the callback to the elements of the array
$newArr = $array->each(function($el){
return $el + 1;
});
var_dump($newArr);
/**
array(2) {
[0]=>
int(2)
[1]=>
int(3)
}
*/
```
In [QuickArray](https://github.com/alirezasalehizadeh/QuickArray) im use this methods in use.