Created
February 24, 2009 06:33
-
-
Save wtnabe/69448 to your computer and use it in GitHub Desktop.
Some useful array functions for PHP
This file contains 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 | |
/** | |
* Array Utils | |
* | |
* @since 2009-02-23 | |
*/ | |
if ( !function_exists( 'array_at' ) ) { | |
/** | |
* array の中の index の位置の値を返す | |
* | |
* $arr[$index] と同じなんだけど、関数の戻り値なども自由に使えるよう | |
* に関数化した | |
* | |
* @since 2009-02-24 | |
* @param int $index | |
* @param array $arr | |
* @return mixed | |
*/ | |
function array_at( $index, $arr ) { | |
if ( !is_int( $index ) ) { | |
trigger_error( "The first argument should be integer.", | |
E_USER_WARNING ); | |
} | |
if ( !is_array( $arr ) ) { | |
trigger_error( "The second argument should be array.", E_USER_WARNING ); | |
} | |
if ( 0 <= $index and $index < sizeof( $arr ) ) { | |
return $arr[$index]; | |
} else { | |
return false; | |
} | |
} | |
} | |
if ( !function_exists( 'array_include' ) ) { | |
/** | |
* 指定した array が元の array に含まれるかどうか | |
* | |
* @since 2009-02-23 | |
* @param array $arr | |
* @param mixed $vals | |
* @return boolean | |
*/ | |
function array_include( $arr, $vals ) { | |
if ( !is_array( $arr ) ) { | |
trigger_error( "The first argument should be array.", E_USER_ERROR ); | |
} | |
$vals = is_array( $vals ) ? $vals : array( $vals ); | |
$inter = array_intersect( $arr, $vals ); | |
if ( is_array( $inter ) ) { | |
return ($vals == array_values( $inter ) ); | |
} else { | |
return false; | |
} | |
} | |
} | |
if ( !function_exists( 'first' ) ) { | |
/** | |
* 配列の最初の値を返す | |
* | |
* @since 2009-02-24 | |
* @param array $arr | |
* @return mixed | |
*/ | |
function first( $arr ) { | |
if ( !is_array( $arr ) ) { | |
trigger_error( "The argument should be array.", E_USER_ERROR ); | |
} | |
reset( $arr ); | |
return current( $arr ); | |
} | |
} | |
if ( !function_exists( 'last' ) ) { | |
/** | |
* 配列の最後の値を返す | |
* | |
* @since 2009-02-24 | |
* @param array $arr | |
* @return mixed | |
*/ | |
function last( $arr ) { | |
if ( !is_array( $arr ) ) { | |
trigger_error( "The argument should be array.", E_USER_ERROR ); | |
} | |
return end( $arr ); | |
} | |
} | |
if ( !function_exists( 'array_delete' ) ) { | |
/** | |
* 配列から指定した値を削除する | |
* | |
* @since 2009-02-24 | |
* @param mixed $vals | |
* @param array $arr | |
* @return array | |
*/ | |
function array_delete( $vals, $arr ) { | |
if ( !is_array( $vals ) ) { | |
$vals = array( $vals ); | |
} | |
if ( !is_array( $arr ) ) { | |
trigger_error( "The second argument should be array.", E_USER_ERROR ); | |
} | |
foreach ( $vals as $val ) { | |
$key = array_search( $val, $arr ); | |
if ( $key !== false and $key !== null ) { | |
array_splice( $arr, $key, 1 ); | |
$arr = array_delete( $val, $arr ); | |
} | |
} | |
return $arr; | |
} | |
} | |
if ( !function_exists( 'array_shuffle' ) ) { | |
/** | |
* 配列を shuffle() して返す | |
* | |
* for PHP 4.3.1 or earlier | |
* | |
* @since 2009-07-28 | |
* @param array $arr | |
* @return array | |
* @see http://en.wikipedia.org/wiki/Fisher–Yates_shuffle | |
*/ | |
function array_shuffle( $arr ) { | |
$i = sizeof( $arr ); | |
while ( $i-- > 0 ) { | |
$j = rand( 0, $i ); | |
// swap | |
$tmp = $arr[$i]; | |
$arr[$i] = $arr[$j]; | |
$arr[$j] = $tmp; | |
} | |
return $arr; | |
} | |
} | |
if ( !function_exists( 'array_sort' ) ) { | |
/** | |
* 配列を非破壊的に sort して返す | |
* | |
* @since 2009-12-12 | |
* @param array $arr | |
* @param string $func | |
* @param const $option | |
* @return array | |
*/ | |
function array_sort( $arr, $func = 'sort', $option = SORT_REGULAR ) { | |
switch ( $func ) { | |
case 'arsort': | |
case 'asort': | |
case 'krsort': | |
case 'ksort': | |
case 'rsort': | |
case 'sort': | |
$func( $arr, $option ); | |
break; | |
case 'natcasesort': | |
case 'natsort': | |
$func( $arr ); | |
break; | |
} | |
return $arr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment