PHP: Array_flip() With Values Containing Arrays
I had the following:
$attributeTypes = array( 'color' => array('background', 'border-color'), 'size' => array('border-width') ); $attributeTypesInverse = array_flip($attributeTypes);But I got this warning: Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! So I wrote a new function to support arrays as values, not just integers or strings. Keep in mind though that the values of the arrays in values must be integers or strings in order to be valid keys for the resulting array.
function array_flip_r($array) { $result = array(); foreach ($array as $k=>$v) { if (is_array($v)) { foreach ($v as $u) { _array_flip_r($result, $k, $u); } } else { _array_flip_r($result, $k, $v); } } return $result; } function _array_flip_r(&$array, $k, $v) { if (is_string($v) || is_int($v)) { $array[$v] = $k; } else { trigger_error("Values must be STRING or INTEGER", E_USER_WARNING); } }