agallou/array-filter-path

Recursively filter an array uning some json-y syntax

0.0.1 2015-11-19 23:36 UTC

This package is auto-updated.

Last update: 2024-10-29 04:41:58 UTC


README

Example

Take for example this array :

$baseArray = array(
  'director' => array(
     'first_name' => 'Robert',
     'last_name' => 'Zemeckis',
  ),
  'actors' => array(
    array(
      'first_name' => 'Michael J.',
      'last_name' => 'Fox',
    ),
    array(
      'first_name' => 'Christopher',
      'last_name' => 'Lloyd',
    ),
  ),
  'label' => 'Back to the Future'
);

If we filter it like this :

use agallou\ArrayFilterPath\ArrayFilterPath as ArrayFilterPath;
$filter = new ArrayFilterPath();
$filters = array(
  'actors[].last_name',
  'label',
);
$filteredArray = $filter->filter($baseArray, $filters);

We will get an array like this, with only the actors last name and the label :

array(
  'actors' => array(
    array(
      'last_name' => 'Fox',
    ),
    array(
      'last_name' => 'Lloyd',
    ),
  ),
  'label' => 'Back to the Future'
);