php陣列常用函數
range ( mixed/*例1*/functioncube( $n ) {return( $n * $n * $n ); } $a = array( 1 , 2 , 3 , 4 , 5 ); $b = array_map ( "cube" , $a ); print_r ( $b ); /** 输出 Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 ) **/?>
登入後複製/*例2*//*将多个数组进行合并*/$a = array( 1 , 2 , 3 , 4 , 5 ); $b = array( "one" , "two" , "three" , "four" , "five" ); $c = array( "uno" , "dos" , "tres" , "cuatro" , "cinco" ); $d = array_map ( null , $a , $b , $c ); print_r ( $d ); /*输出 Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] => Array ( [0] => 4 [1] => four [2] => cuatro ) [4] => Array ( [0] => 5 [1] => five [2] => cinco ) ) */?>
登入後複製
/* 例1,产生一组数字 */$nums = range(1, 5); print_r($nums); /* 输出: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) */$nums = range(1, 5, 2); print_r($nums); /* 输出: Array ( [0] => 1 [1] => 3 [2] => 5 ) */
/* 例2,产生一组字母数组 */$array = range('a','f'); print_r($array); /* 输出: Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f ) */$array = array('a', 'f', 2); print_r($array); /* 输出: print_r($array); /* 输出: Array ( [0] => a [1] => c [2] => e ) */
a
/* 解释:如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。 */$array1 = array(1, 2, 3, 4, 5); $array2 = array(1, 2, 8, 9); $array3 = array_merge($array1, $array2); print_r($array3); /* 输出: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 1 [6] => 2 [7] => 8 [8] => 9 ) */
a r
/* 例1 */$ar1 = array("color" => array("favorite" => "red"), 5); $ar2 = array(10, "color" => array("favorite" => array('a'=>"red"), "blue")); $result = array_merge_recursive($ar1, $ar2); print_r($result); /* 输出: Array ( [color] => Array ( [favorite] => Array ( [0] => red [a] => red 重点 ) [0] => blue ) [0] => 5 [1] => 10 ) */
/* 例2 */$ar1 = array("color" => array("favorite" => "red"), 5); $ar2 = array(10, "color" => array("favorite" =>"red", "blue")); $result = array_merge_recursive($ar1, $ar2); print_r($result); /* 输出: Array ( [color] => Array ( [favorite] => Array ( [0] => red [1] => red 重点 ) [0] => blue ) [0] => 5 [1] => 10 ) */
t
$input = array(12, 10, 9); $result = array_pad($input, 5, 0); // result is array(12, 10, 9, 0, 0)$result = array_pad($input, -7, -1); // result is array(-1, -1, -1, -1, 12, 10, 9)$result = array_pad($input, 2, "noop"); // not padded/* 如果size<数组的长度,将不会有变化 */?>
array_reverse()
array_sum()
usort()
rsort()
ksort()
krsort()
以上就介紹了php常用陣列函數查詢手冊,包含了陣列函數,php方面的內容,希望對PHP教學有興趣的朋友有幫助。