Paste P6435

php foreach vs array_filter
ActivePublic

Authored by dcausse on Dec 6 2017, 2:03 PM.
1$namespaces = array_filter( $namespaces, function ( $k, $v ) {
2wfDebug( "via array_filter: " . gettype( $v ) . " : " . gettype( NS_MAIN ) );
3return true; // $v !== NS_MAIN;
4}, ARRAY_FILTER_USE_BOTH );
5wfDebug( "VIA reset: " . gettype( reset( $namespaces ) ) );
6wfDebug( "DUMP:" . print_r( $namespaces, true ) );
7foreach ( $namespaces as $k => $v ) {
8wfDebug( "$k => $v | " . NS_MAIN );
9wfDebug( "via foreach:" . gettype( $v ) . " : " . gettype( NS_MAIN ) );
10if ( $v === NS_MAIN ) {
11wfDebug( "UNSET" . print_r($namespaces, true) );
12unset( $namespaces[$k] );
13}
14}
15wfDebug( print_r($namespaces, true) );
16
17will output:
182017-12-06 13:59:20 cirrustestwiki wfDebug DEBUG: via array_filter: integer : integer
192017-12-06 13:59:20 cirrustestwiki wfDebug DEBUG: VIA reset: string
202017-12-06 13:59:20 cirrustestwiki wfDebug DEBUG: DUMP:Array
21(
22[0] => 0
23)
242017-12-06 13:59:20 cirrustestwiki wfDebug DEBUG: 0 => 0 | 0
252017-12-06 13:59:20 cirrustestwiki wfDebug DEBUG: via foreach:string : integer
262017-12-06 13:59:20 cirrustestwiki wfDebug DEBUG: Array
27(
28[0] => 0
29)
dcausse created this paste.Dec 6 2017, 2:03 PM