helpers\BaseArrayHelper filter()

filter() public static method (available since version 2.0.9)

Filters array according to rules specified.

For example:

$array = [
    'A' => [1, 2],
    'B' => [
        'C' => 1,
        'D' => 2,
    ],
    'E' => 1,
];

$result = \yii\helpers\ArrayHelper::filter($array, ['A']);
// $result will be:
// [
//     'A' => [1, 2],
// ]

$result = \yii\helpers\ArrayHelper::filter($array, ['A', 'B.C']);
// $result will be:
// [
//     'A' => [1, 2],
//     'B' => ['C' => 1],
// ]

$result = \yii\helpers\ArrayHelper::filter($array, ['B', '!B.C']);
// $result will be:
// [
//     'B' => ['D' => 2],
// ]
public static array filter ( $array, $filters )
$array array

Source array

$filters array

Rules that define array keys which should be left or removed from results. Each rule is:

  • var - $array['var'] will be left in result.
  • var.key = only `$array['var']['key'] will be left in result.
  • !var.key = `$array['var']['key'] will be removed from result.
return array

Filtered array

doc_Yii
2016-10-30 17:04:25
Comments
Leave a Comment

Please login to continue.