value pairs from haystack * Useful for filtering results to a select box, like status. * @param $needles * @param $haystack * @return array|bool */ static function getByArray( $needles, $haystack ) { if ( !is_array( $needles ) ) { $needles = [ $needles ]; } $needles = array_unique( $needles ); $retval = []; foreach ( $needles as $needle ) { if ( isset( $haystack[$needle] ) ) { $retval[$needle] = $haystack[$needle]; } } if ( empty( $retval ) == false ) { return $retval; } return false; } /** * @param $bitmask * @param $options * @return array|bool */ static function getArrayByBitMask( $bitmask, $options ) { $bitmask = (int)$bitmask; $retarr = []; if ( is_numeric( $bitmask ) && is_array( $options ) ) { foreach ( $options as $key => $value ) { //Debug::Text('Checking Bitmask: '. $bitmask .' mod '. $key .' != 0', __FILE__, __LINE__, __METHOD__, 10); if ( ( $bitmask & (int)$key ) !== 0 ) { //Debug::Text('Found Bit: '. $key, __FILE__, __LINE__, __METHOD__, 10); $retarr[] = $key; } } unset( $value ); //code standards } if ( empty( $retarr ) == false ) { return $retarr; } return false; } /** * @param $keys * @param $options * @return int|mixed */ static function getBitMaskByArray( $keys, $options ) { //If an integer is passed in try and convert it to an array automatically. if ( is_numeric( $keys ) == true ) { $keys = self::getArrayByBitMask( $keys, $options ); } $retval = 0; if ( is_array( $keys ) && is_array( $options ) ) { foreach ( $keys as $key ) { if ( isset( $options[$key] ) ) { $retval |= $key; } else { Debug::Text( 'Key is not a valid bitmask int: ' . $key, __FILE__, __LINE__, __METHOD__, 10 ); } } } return $retval; } } ?>