PHPackages                             newnakashima/typedarray - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. newnakashima/typedarray

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

newnakashima/typedarray
=======================

Typed Array for PHP

0.1.8(2y ago)079MITPHP

Since Aug 1Pushed 2y ago1 watchersCompare

[ Source](https://github.com/newnakashima/TypedArray)[ Packagist](https://packagist.org/packages/newnakashima/typedarray)[ RSS](/packages/newnakashima-typedarray/feed)WikiDiscussions main Synced today

READMEChangelog (9)Dependencies (1)Versions (11)Used By (0)

TypedArray
==========

[](#typedarray)

Typed Array for PHP.

TypedArray object can be treated as an array. But it has a same type for every element.

For example, a TypedArray object which has string type does not allow developers to add an integer value to it.

```
$stringArray = new TypedArray('string', ['foo', 'bar']);

// This causes an error.
$stringArray[] = 42;

// This is OK.
$stringArray[] = 'baz';
```

Example
-------

[](#example)

### Initialize

[](#initialize)

```
$length = 10000;
# with initial value.
$list = new TypedArray(Primitives::Int->value, range(1, $length));

# without initial value.
$empty = new TypedArray('int');
foreach ($list as $item) {
    $empty->add($item * 2);
}
assert($empty->count() === $length);
```

### primitive types

[](#primitive-types)

#### integer

[](#integer)

```
$intArray = new TypedArray(Primitives::Int->value, range(1, 100));
# or
$intArray = new TypedArray('int', range(1, 100));
# or
$intArray = new TypedArray('integer', range(1, 100));
```

#### float(double)

[](#floatdouble)

```
$doubleArray = new TypedArray(Primitives::Float->value, [0.1, 1.2]);
#or
$doubleArray = new TypedArray('float', [0.1, 1.2]);
#or
$doubleArray = new TypedArray('double', [0.1, 1.2]);
```

#### string

[](#string)

```
$stringArray = new TypedArray(Primitives::String->value, ['foo', 'bar']);
# or
$stringArray = new TypedArray('string', ['foo', 'bar']);
```

#### bool

[](#bool)

```
$boolArray = new TypedArray(Primitives::Bool->value, [true, false]);
# or
$boolArray = new TypedArray('bool', [true, false]);
```

#### array

[](#array)

```
$arrayArray = new TypedArray(Primitives::Array->vale, [
    [1, 2, 3],
    ['foo', 'bar', 'baz'],
]);
# or
$arrayArray = new TypedArray('array', [
    [1, 2, 3],
    ['foo', 'bar', 'baz'],
]);
```

#### object

[](#object)

```
$obj1 = new \stdClass();
$obj2 = new \stdClass();
$objectArray = new TypedArray(Primitives::Object->value, [$obj1, $obj2]);
# or
$objectArray = new TypedArray('object', [$obj1, $obj2]);
```

#### other classes or interfaces

[](#other-classes-or-interfaces)

```
class Foo
{
    public int $value = 0;
    public function __construct($value)
    {
        $this->value = $value;
    }
}

$fooArray = new TypedArray(Foo::class, [
    new Foo(1),
    new Foo(2),
]);
```

If the class takes only one argument for constructor, you can omit `new` keyword and class name.

```
$fooArray = new TypedArray(Foo::class, [1, 2]);
assert(get_class($fooArray[0]) == Foo::class);
```

### add value

[](#add-value)

```
$list = new TypedArray('string');
$list[] = 'foo';
# or
$list->add('foo');
```

### map

[](#map)

This method returns a primitive array. Not a TypedArray object.

```
$mapped = $list->map(fn ($item) => $item * 2);
assert($mapped[$length - 1], $length * 2);
```

### mapWithType

[](#mapwithtype)

This method returns another TypedArray which has a specific type.

```
$list = new TypedArray('string', ['a', 'bb', 'cccc']);
$mapped = $list->mapWithType('int', fn ($item) => strlen($item));
assert(get_class($mapped) === TypedArray::class);
assert($mapped[0] === 1);
assert($mapped[1] === 2);
assert($mapped[2] === 4);
```

### mapWithSameType

[](#mapwithsametype)

```
$list = new TypedArray('string', ['a', 'bb', 'cccc']);
$mapped = $list->mapWithSameType(fn ($item) => $item . '!');
assert(get_class($mapped) === TypedArray::class);
assert($mapped[0] === 'a!');
assert($mapped[1] === 'bb!');
assert($mapped[2] === 'cccc!');
```

### filter

[](#filter)

```
$filtered = $list->filter(fn ($item) => $item % 2 === 0);
assert($filtered->count() === $length / 2);
```

### each

[](#each)

```
$filtered->each(fn ($item) => assert($item % 2 === 0));
```

### merge

[](#merge)

```
$list = new TypedArray('string', ['foo', 'bar']);
$anotherList = new TypedArray('string', ['baz', 'qux']);
$list = $list->merge($anotherList);
assert($list->count() === 4);
assert($list[0] === 'foo');
assert($list[1] === 'bar');
assert($list[2] === 'baz');
assert($list[3] === 'qux');
```

### find

[](#find)

```
$list = new TypedArray('string', ['foo', 'bar']);
$found = $list->find(fn ($item) => $item === 'bar');
assert($found === 'bar');
```

### push

[](#push)

Wrapper of array\_push() function.

```
$list = new TypedArray('string', ['foo', 'bar']);
$list->push('baz', 'qux');
assert($list->count() === 4);
assert($list[2] === 'baz');
assert($list[3] === 'qux');
```

### pop

[](#pop)

```
$list = new TypedArray('string', ['foo', 'bar', 'baz']);
$last = $list->pop();
assert($last === 'baz');
assert($list->count() === 2);
```

### shift

[](#shift)

```
$list = new TypedArray('string', ['foo', 'bar', baz]);
$first = $list->shift();
assert($first === 'foo');
assert($list->count() === 2);
```

### unshift

[](#unshift)

```
$list = new TypedArray('string', ['foo', 'bar']);
$list->unshift('baz', 'qux');
assert($list->count() === 4);
assert($list[0] === 'baz');
```

### reverse

[](#reverse)

```
$list = new TypedArray('string', ['foo', 'bar', 'baz']);
$reversed = $list->reverse();
assert($reversed[0] === 'baz');
assert($reversed[1] === 'bar');
assert($reversed[2] === 'foo');
```

### first

[](#first)

```
$list = new TypedArray('string', ['foo', 'bar', 'baz']);
$first = $list->first();
assert($first === 'foo');
```

### last

[](#last)

```
$list = new TypedArray('string', ['foo', 'bar', 'baz']);
$last = $list->last();
assert($last === 'baz');
```

### toArray

[](#toarray)

```
$list = new TypedArray('string', ['foo', 'bar', 'baz']);
$primitiveArray = $list->toArray();
assert(is_array($primitiveArray));
var_dump($primitiveArray);
/**
 * array(3) {
 *   [0]=>
 *   string(3) "foo"
 *   [1]=>
 *   string(3) "bar"
 *   [2]=>
 *   string(3) "baz"
 * }
 */
```

### \_\_toString

[](#__tostring)

This method returns a string representation for debugging.

```
$list = new TypedArray('string', ['foo', 'bar', 'baz']);
$string = (string)$list
echo $string;
/**
 * string(51) "array (
 *   0 => 'foo',
 *   1 => 'bar',
 *   2 => 'baz',
 * )"
 */
```

Auto initialization
-------------------

[](#auto-initialization)

If you pass a value which is not an instance of the given type, TypedArray will try to initialize the object with the given value.

```
$exampleArray = new TypedArray(Foo::class, [
    new Foo(1),
    new Foo(2),
]);

// This causes an error because the type is not matched.
$exampleArray->add(new Bar(3));

// This is OK because the type is matched.
$exampleArray->add(new Foo(3));

// This is OK too. TypedArray will try to initialize the object with the given value.
// Within add() method, Foo::__construct(3) is called.
$exampleArray->add(3);

assert($exampleArray[2]->value === 3);
assert($exampleArray[2] instanceof Foo);
```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

10

Last Release

1051d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/12591755?v=4)[Tsuyoshi Nakashima](/maintainers/newnakashima)[@newnakashima](https://github.com/newnakashima)

---

Top Contributors

[![newnakashima](https://avatars.githubusercontent.com/u/12591755?v=4)](https://github.com/newnakashima "newnakashima (36 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/newnakashima-typedarray/health.svg)

```
[![Health](https://phpackages.com/badges/newnakashima-typedarray/health.svg)](https://phpackages.com/packages/newnakashima-typedarray)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
