Make WordPress Core

Ticket #16378: 16378.compat.diff

File 16378.compat.diff, 1.1 KB (added by johnjamesjacoby, 12 years ago)

Add array_intersect_key to compat.php

  • wp-includes/compat.php

     
    168168        }
    169169        return $parts;
    170170}
     171
     172/**
     173 * PHP 4 compat version of array_intersect_key() which was introduced
     174 * in PHP 5.0.2. This version requires at least PHP 4.0.0.
     175 *
     176 * @since WordPress 3.1
     177 */
     178if ( !function_exists( 'array_intersect_key' ) ) {
     179        function array_intersect_key() {
     180                $args        = func_get_args();
     181                $array_count = count( $args );
     182
     183                if ( $array_count < 2 ) {
     184                        user_error( 'Wrong parameter count for array_intersect_key()', E_USER_WARNING );
     185                        return;
     186                }
     187
     188                // Check arrays
     189                for ( $i = $array_count; $i--; ) {
     190                        if ( !is_array( $args[$i] ) ) {
     191                                user_error( 'array_intersect_key() Argument #' . ( $i + 1 ) . ' is not an array', E_USER_WARNING );
     192                                return;
     193                        }
     194                }
     195
     196                // Intersect keys
     197                $arg_keys    = array_map( 'array_keys', $args );
     198                $result_keys = call_user_func_array( 'array_intersect', $arg_keys );
     199
     200                // Build return array
     201                $result = array( );
     202                foreach ( $result_keys as $key )
     203                        $result[$key] = $args[0][$key];
     204
     205                return $result;
     206        }
     207
     208}