PHPackages                             mtownsend/array-redactor - 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. [Logging &amp; Monitoring](/categories/logging)
4. /
5. mtownsend/array-redactor

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

mtownsend/array-redactor
========================

A PHP package to redact array values by their keys.

1.0.1(6y ago)146112.3k↓19.8%9[2 PRs](https://github.com/mtownsend5512/array-redactor/pulls)1MITPHPPHP &gt;=5.6

Since May 21Pushed 2y ago2 watchersCompare

[ Source](https://github.com/mtownsend5512/array-redactor)[ Packagist](https://packagist.org/packages/mtownsend/array-redactor)[ RSS](/packages/mtownsend-array-redactor/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (1)

A PHP package to redact an array's values by their keys no matter how deep the array.

[![](https://camo.githubusercontent.com/5f653887ec3d95af09a5291f2b6508f459f2e396a3106e200b720d8b1d94fa78/68747470733a2f2f692e696d6775722e636f6d2f7a6b524e6932412e6a7067)](https://camo.githubusercontent.com/5f653887ec3d95af09a5291f2b6508f459f2e396a3106e200b720d8b1d94fa78/68747470733a2f2f692e696d6775722e636f6d2f7a6b524e6932412e6a7067)

Why?
----

[](#why)

Have you ever built or interacted with an api and needed to log all outgoing and incoming calls? Chances are that somewhere in that process is an authentication, either by an app or on behalf of a user. Logs are useful for debugging, but storing sensitive information such as passwords or api keys is not something you want to have in your logs for anyone to see. The usage goes beyond just this example, but that is what prompted me to create the ArrayRedactor package.

Whatever your usage needs may be, this package aims to provide a dead-simple, lightweight way to censor sensitive information in an array no matter how deeply it is nested.

Installation
------------

[](#installation)

Install via composer:

```
composer require mtownsend/array-redactor

```

*This package is designed to work with any PHP 5.6+ application but has special Facade support for Laravel.*

### Registering the service provider (Laravel users)

[](#registering-the-service-provider-laravel-users)

For Laravel 5.4 and lower, add the following line to your `config/app.php`:

```
/*
 * Package Service Providers...
 */
Mtownsend\ArrayRedactor\Providers\ArrayRedactorServiceProvider::class,
```

For Laravel 5.5 and greater, the package will auto register the provider for you.

### Using Lumen

[](#using-lumen)

To register the service provider, add the following line to `app/bootstrap/app.php`:

```
$app->register(Mtownsend\ArrayRedactor\Providers\ArrayRedactorServiceProvider::class);
```

### Publishing the config file (Laravel users)

[](#publishing-the-config-file-laravel-users)

```
php artisan vendor:publish --provider="Mtownsend\ArrayRedactor\Providers\ArrayRedactorServiceProvider"

```

Once your `arrayredactor.php` has been published to your config folder, you will see a file with 2 keys in it: `keys` and `ink`. You can replace these values with anything you want, but please note: **these values will only be applied when using the Laravel Facade**.

Quick start
-----------

[](#quick-start)

### Using the class

[](#using-the-class)

```
use Mtownsend\ArrayRedactor\ArrayRedactor;

// An example array, maybe a request being made to/from an API application you wish to log in your database
$login = [
    'email' => 'john_doe@domain.com',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];

$redactor = (new ArrayRedactor($login, ['password', 'session_id']))->redact();

// $redactor will return:
[
    'email' => 'john_doe@domain.com',
    'password' => '[REDACTED]',
    'data' => [
        'session_id' => '[REDACTED]'
    ],
];
```

### Advanced usage

[](#advanced-usage)

Array Redactor can also receive valid json instead of an array of content.

```
$json = json_encode([
    'email' => 'john_doe@domain.com',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
]);

$redactor = (new ArrayRedactor($json, ['password', 'session_id']))->redact();

// $redactor will return:
[
    'email' => 'john_doe@domain.com',
    'password' => '[REDACTED]',
    'data' => [
        'session_id' => '[REDACTED]'
    ],
];
```

You can also receive your content back as json instead of an array.

```
$login = [
    'email' => 'john_doe@domain.com',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];

$redactor = (new ArrayRedactor($login, ['password', 'session_id']))->redactToJson();

// $redactor will return:
"{
	"email": "john_doe@domain.com",
	"password": "[REDACTED]",
	"data": {
		"session_id": "[REDACTED]"
	}
}"
```

You can change the redaction value (default: \[REDACTED\]), known as the `ink`, by passing it as the third argument of the constructor, or using the dedicated `->ink()` method.

```
$login = [
    'email' => 'john_doe@domain.com',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];

$redactor = (new ArrayRedactor($login, ['password', 'session_id'], null))->redact();
// or...
$redactor = (new ArrayRedactor($login, ['password', 'session_id']))->ink(null)->redact();

// $redactor will return:
[
    'email' => 'john_doe@domain.com',
    'password' => null,
    'data' => [
        'session_id' => null
    ],
];
```

You can call the `ArrayRedactor` as a function and the magic `__invoke()` method will call the `redact` method for you.

```
$login = [
    'email' => 'john_doe@domain.com',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];

$redactor = (new ArrayRedactor($login, ['password', 'session_id'], null))();

// $redactor will return:
[
    'email' => 'john_doe@domain.com',
    'password' => null,
    'data' => [
        'session_id' => null
    ],
];
```

Lastly, you can skip the constructor arguments entirely if you prefer.

```
$login = [
    'email' => 'john_doe@domain.com',
    'password' => 'secret123',
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];

$redactor = (new ArrayRedactor)->content($login)->keys(['password'])->ink(null)->redact();

// $redactor will return:
[
    'email' => 'john_doe@domain.com',
    'password' => null,
    'data' => [
        'session_id' => 'z481jf0an4kasnc8a84aj831'
    ],
];
```

### Using the global helper

[](#using-the-global-helper)

This package provides a convenient helper function which is globally accessible.

```
array_redactor($array, $keys, $ink)->redact();
// or...
array_redactor()->content($array)->keys(['current_password', 'new_password'])->ink('████████')->redact();
```

### Using the facade (Laravel users)

[](#using-the-facade-laravel-users)

If you are using Laravel, this package provides a facade. To register the facade add the following line to your `config/app.php` under the `aliases` key.

**Please note:** this is the only method for Laravel users that will prefill your `keys` and `ink` from your `arrayredactor.php` config file. The global helper and direct instantiation of the class will not prefill these values for you.

```
'ArrayRedactor' => Mtownsend\ArrayRedactor\Facades\ArrayRedactor::class,
```

```
use ArrayRedactor;

// Laravel prefills our keys() and ink() methods for us from the config file
ArrayRedactor::content($array)->redact();
```

Error handling
--------------

[](#error-handling)

In the event you pass content that is not valid json or an array, an `ArrayRedactorException` will be thrown.

```
try {
    $redactor = (new ArrayRedactor('i am an invalid argument', ['password']))->redact();
} catch (\Mtownsend\ArrayRedactor\Exceptions\ArrayRedactorException $exception) {
    // do something...
}
```

Credits
-------

[](#credits)

- Mark Townsend
- [All Contributors](../../contributors)

Testing
-------

[](#testing)

You can run the tests with:

```
./vendor/bin/phpunit
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 71.4% 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 ~16 days

Total

2

Last Release

2537d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/812987?v=4)[mtownsend](/maintainers/mtownsend)[@mtownsend](https://github.com/mtownsend)

---

Top Contributors

[![mtownsend5512](https://avatars.githubusercontent.com/u/4945553?v=4)](https://github.com/mtownsend5512 "mtownsend5512 (5 commits)")[![AliN11](https://avatars.githubusercontent.com/u/16048964?v=4)](https://github.com/AliN11 "AliN11 (1 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (1 commits)")

---

Tags

arraylaravelloggingrecursiveredactorlogarrayloggerrecursivesensitivecensorRedactorredact

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mtownsend-array-redactor/health.svg)

```
[![Health](https://phpackages.com/badges/mtownsend-array-redactor/health.svg)](https://phpackages.com/packages/mtownsend-array-redactor)
```

###  Alternatives

[analog/analog

Fast, flexible, easy PSR-3-compatible PHP logging package with dozens of handlers.

3451.5M24](/packages/analog-analog)[theorchard/monolog-cascade

Monolog extension to configure multiple loggers in the blink of an eye and access them from anywhere

1482.2M9](/packages/theorchard-monolog-cascade)[inpsyde/wonolog

Monolog-based logging package for WordPress.

183617.9k7](/packages/inpsyde-wonolog)[amphp/log

Non-blocking logging for PHP based on Amp, Revolt, and Monolog.

402.6M70](/packages/amphp-log)[apix/log

Minimalist, thin and fast PSR-3 compliant (multi-bucket) logger.

511.0M18](/packages/apix-log)[logtail/monolog-logtail

Logtail handler for Monolog

233.2M3](/packages/logtail-monolog-logtail)

PHPackages © 2026

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