MediaWiki REL1_31
wfArrayFilterTest.php
Go to the documentation of this file.
1<?php
2
8class WfArrayFilterTest extends \PHPUnit\Framework\TestCase {
9 public function testWfArrayFilter() {
10 $arr = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
11 $filtered = wfArrayFilter( $arr, function ( $val, $key ) {
12 return $key !== 'b';
13 } );
14 $this->assertSame( [ 'a' => 1, 'c' => 3 ], $filtered );
15
16 $arr = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
17 $filtered = wfArrayFilter( $arr, function ( $val, $key ) {
18 return $val !== 2;
19 } );
20 $this->assertSame( [ 'a' => 1, 'c' => 3 ], $filtered );
21
22 $arr = [ 'a', 'b', 'c' ];
23 $filtered = wfArrayFilter( $arr, function ( $val, $key ) {
24 return $key !== 0;
25 } );
26 $this->assertSame( [ 1 => 'b', 2 => 'c' ], $filtered );
27 }
28
29 public function testWfArrayFilterByKey() {
30 $arr = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
31 $filtered = wfArrayFilterByKey( $arr, function ( $key ) {
32 return $key !== 'b';
33 } );
34 $this->assertSame( [ 'a' => 1, 'c' => 3 ], $filtered );
35
36 $arr = [ 'a', 'b', 'c' ];
37 $filtered = wfArrayFilterByKey( $arr, function ( $key ) {
38 return $key !== 0;
39 } );
40 $this->assertSame( [ 1 => 'b', 2 => 'c' ], $filtered );
41 }
42}
wfArrayFilterByKey(array $arr, callable $callback)
Like array_filter with ARRAY_FILTER_USE_KEY, but works pre-5.6.
wfArrayFilter(array $arr, callable $callback)
Like array_filter with ARRAY_FILTER_USE_BOTH, but works pre-5.6.
GlobalFunctions wfArrayFilter wfArrayFilterByKey.