php tips
PHP tips
- hash a flatfile-like array (like the results of a sql join) into a normalized tree structure based on whatever keys you like
- Contributed to php.net
function hashByFields($things,$fields) { $retval=null; $count = count($fields); foreach ($things as $thing) { $fwibble =& $retval; for ($j=0;$j<$count;$j++) { $val = $thing[$fields[$j]]; if ($j == $count - 1) { $fwibble[$val] = $thing; } else if (!isset($fwibble[$val])) { $fwibble[$val] = Array(); } $fwibble =& $fwibble[$val]; } } return $retval; } - sort an array of associative arrays by any associative value
- Contributed to php.net (note the todo there in the body)
function assocMultiSort(&$array,$keys,$directions=true) { if (!is_array($array) || count($array) == 0) return true; if (!is_array($keys)) { $keys = Array($keys); } $test = current($array); $assocSortCompare = ''; for ($i=0,$count=count($keys);$i<$count;$i++) { $key = $keys[$i]; if (is_array($directions)) { $direction = $directions[$i]; } else { $direction = $directions; } if ($i > 0) $assocSortCompare .= 'if ($retval != 0) return $retval; '; $assocSortCompare .= '$ax = $a["'.$key.'"]; $bx = $b["'.$key.'"];'; //we want "current" instead of necessarily the "zeroth" element; there may not be a zeroth element //TODO -- if it's "blank", search up the list until we find something not blank if (is_numeric($test[$key]) || ($test[$key] == ((int)$test[$key]))) { if ($direction) { $assocSortCompare.= ' $retval = ($ax == $bx) ? 0 : (($ax < $bx) ? -1 : 1);'; } else { $assocSortCompare.= ' $retval = ($ax == $bx) ? 0 : (($ax < $bx) ? 1 : -1);'; } } else { if ($direction) { $assocSortCompare.= ' $retval = strcmp($ax,$bx);'; } else { $assocSortCompare.= ' $retval = strcmp($bx,$ax);'; } } } $assocSortCompare.= ' return $retval;'; $assocSortCompare = create_function('$a,$b',$assocSortCompare); $retval = usort($array,$assocSortCompare); return $retval; } - sort an array of associative arrays by any array of associative values
- Contributed to php.net (note: I need to update this to not depend on $array[0])
function assocSort(&$array,$key) { if (!is_array($array) || count($array) == 0) return true; $assocSortCompare = '$a = $a["'.$key.'"]; $b = $b["'.$key.'"];'; if (is_numeric($array[0][$key])) { $assocSortCompare.= ' return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);'; } else { $assocSortCompare.= ' return strcmp($a,$b);'; } $assocSortCompare = create_function('$a,$b',$assocSortCompare); return usort($array,$assocSortCompare); } - test for valid US phone number, and get it back formatted at the same time
- Contributed to php.net
function getUSPhone($var) { $US_PHONE_PREG ="/^(?:\+?1[\-\s]?)?(\(\d{3}\)|\d{3})[\-\s\.]?"; //area code $US_PHONE_PREG.="(\d{3})[\-\.]?(\d{4})"; // seven digits $US_PHONE_PREG.="(?:\s?x|\s|\s?ext(?:\.|\s)?)?(\d*)?$/"; // any extension if (!preg_match($US_PHONE_PREG,$var,$match)) { return false; } else { $tmp = "+1 "; if (substr($match[1],0,1) == "(") { $tmp.=$match[1]; } else { $tmp.="(".$match[1].")"; } $tmp.=" ".$match[2]."-".$match[3]; if ($match[4] <> '') $tmp.=" x".$match[4]; return $tmp; } }usage:$phone = $_REQUEST["phone"]; if (!($phone = getUSPhone($phone))) { //error gracefully :) }