agallou/array-filter-path

递归地使用类似JSON的语法过滤数组

0.0.1 2015-11-19 23:36 UTC

This package is auto-updated.

Last update: 2024-08-29 04:19:29 UTC


README

示例

例如这个数组

$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'
);

如果我们这样过滤它

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

我们将得到一个这样的数组,只包含演员的姓氏和标签

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