Skip to content

extract()

Read multiple paths in one call and return a flat map.

Signature

php
JsonFast::extract(
    string $json,
    array $paths,
    ?int $output = OUTPUT_ARRAY
): mixed

Parameters

NameTypeDefaultDescription
$jsonstringJSON document
$pathsarrayList of path strings
$output?intOUTPUT_ARRAYOutput mode

Returns

Associative map keyed by path string. Missing paths map to null.

Examples

php
$json = '{"user":{"name":"Allan","email":"allan@example.com"}}';

$result = JsonFast::extract($json, [
    'user.name',
    'user.email',
    'user.phone',
]);
/*
[
    'user.name' => 'Allan',
    'user.email' => 'allan@example.com',
    'user.phone' => null,
]
*/

Projection for APIs:

php
$view = JsonFast::extract($document, [
    'meta.version',
    'meta.updated',
    'data.summary',
]);

String output for each extracted subtree:

php
$parts = JsonFast::extract($json, ['config', 'secrets'], JsonFast::OUTPUT_STRING);

vs multiple get() calls

One native pass over the document — useful when you need several fields from a large payload and want to avoid repeated full decodes in PHP.

Native tools, weird experiments, and practical performance work.