Created
June 12, 2012 13:17
-
-
Save s-tajima/2917460 to your computer and use it in GitHub Desktop.
PHP snipets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//ユーザ情報配列からidだけ抜き出す | |
$users = array( | |
array('id' => 1, 'name' => 'name1'), | |
array('id' => 2, 'name' => 'name2'), | |
array('id' => 3, 'name' => 'name3'), | |
array('id' => 4, 'name' => 'name4') | |
); | |
$ids = array_map(create_function('$var', 'return $var["id"];'), $users); | |
print_r($ids); | |
//includeのパスを追加する。 | |
$path = '/usr/lib/pear'; | |
set_include_path($path . PATH_SEPARATOR . get_include_path()); | |
//objectをarrayに変換する | |
function obj_to_array($obj){ | |
if(is_object($obj)){ | |
return array_map("obj_to_array", (array)$obj); | |
}elseif(is_array($obj)){ | |
return array_map("obj_to_array", $obj); | |
}else{ | |
return $obj; | |
} | |
} | |
#絶対値を使ったsort | |
function cmp($a, $b) | |
{ | |
$a = abs($a); | |
$b = abs($b); | |
if ($a == $b) return 0; | |
return ($a < $b) ? -1 : 1; | |
} | |
usort($this->stack, "cmp"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment