Arrays

PHP Array Helper Functions

Helpers for selecting allowed keys, grouping records and filtering null values.

Overview

Helpers for selecting allowed keys, grouping records and filtering null values.

Code example

<?php
declare(strict_types=1);
function onlyKeys(array $data,array $allowed): array { return array_intersect_key($data,array_flip($allowed)); }
function groupByKey(array $items,string $key): array {
    $groups=[]; foreach($items as $item) if(array_key_exists($key,$item)) $groups[(string)$item[$key]][]=$item;
    return $groups;
}
function withoutNulls(array $data): array { return array_filter($data,static fn($v)=>$v!==null); }

How to use it

Place the snippet in a namespaced helper or service file, adapt its inputs to your application and add tests for expected and edge-case behavior.

Notes

The example is intentionally small. Validate untrusted input, handle exceptions and document any PHP extension or database requirements.