php常用函数-数组 php常用字符串函数 php常用类库 php常用英语单

WBOY
Freigeben: 2016-07-29 08:50:42
Original
1083 Leute haben es durchsucht

学习php的过程中,整理的一些常用的函数,这是数组函数。

//array():生成一个数组
$a = array("dog","cat","horse");
print_r($a); //Array ( [0] => dog [1] => cat [2] => horse )
//array_combine():生成一个数组,用一个数组的值作为键名,另一个数组的值作为值
$a1 = array("a","b","c","d");
$a2 = array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2)); //Array ( [a] => Cat [b] => Dog [c] => Horse [d] => Cow )
//range():创建并返回一个包含指定范围的元素的数组。
$number = range(0,50,10); //(0:序列的第一个值;50:序列结束值;10:每次的步长)
print_r ($number); //Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )
//compact():创建一个由参数所带变量组成的数组
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = compact("firstname", "lastname", "age");
print_r($result); //Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 )
//array_fill():用给定的值生成数组
$a = array_fill(2,3,"Dog"); //(2:填冲的第一个键值;3:填冲的数值;dog:填冲的内容)
print_r($a); //Array ( [2] => Dog [3] => Dog [4] => Dog )
//array_chunk():把一个数组分割为新的数组块
$a = array("a"=>"cat","b"=>"dog","c"=>"horse","d"=>"Cow");
print_r(array_chunk($a,2)); //Array([0] => Array([a]=>cat [b]=>dog) [1] => Array([c]=>horse [d]=>cow))
//array_merge():把两个数组合并为一个数组
/***********************与array_combine区别***********************
array_merge():直接合并数组;array_combine():根据参数顺序第一组为键,第二组为值;*/
echo "


";
$a1 = array("a"=>"Horse","b"=>"Dog");
$a2 = array("c"=>"Cow","b"=>"cat");
print_r(array_merge($a1,$a2)); //Array ( [a] => Horse [b] => Dog [c] => Cow [d] => cat )
//array_diff():返回两个数组的差集(键名保持不变)
$a1 = array(8=>"Cat",1=>"Dog",2=>"Horse",3=>"lion");
$a2 = array(4=>"Horse",5=>"Dog",6=>"bird",7=>"pig");
print_r(array_diff($a1,$a2)); //Array ( [8] => Cat [3] => lion )
print_r(array_diff($a2,$a1)); //Array ( [6] => bird [7] => pig )
//array_intersect():返回两个或多个数组的交集数组
$a1 = array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2 = array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_intersect($a1,$a2)); // Array ( [1] => Dog [2] => Horse )
print_r(array_intersect($a2,$a1)); // Array ( [3] => Horse [4] => Dog )
//array_serach在数组中搜索给定的值,如果成功则返回相应的键名(失败返回false)
$a = array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_search("Dog",$a); //a
//array_slice():在数组中根据条件取出一段值,并返回(键名保持不变)
echo "
";
$a = array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird");
print_r(array_slice($a,1,2)); //1:从键值(相当于索引键为1的位置开始取);2,取两个
//Array ( [b] => Cat [c] => Horse )
//array_splice():把数组中的一部分去掉,并用其它的值取代
$a1 = array(4=>"Dog",'b'=>"Cat",'c'=>"Horse",6=>"Bird");
$a2 = array(3=>"Tiger",5=>"Lion");
array_splice($a1,1,2,$a2);
/* $a1:被替换的数组(最后被输出的数组);1:按索引键的1位置开始替换;2:替换两个;$a2:替换数组,加到$a1中去 */
print_r($a1); //Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Bird )
//array_splice($a1,0,2,$a2);
//print_r($a1); // Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
//array_sum():计算数组中所有值的和
$a = array(0=>"5",1=>"15",2=>"25");
echo array_sum($a); //45
//in_array():检查数组中是否存在某个值
$animals = array("dog", "cat", "cow", "horse");
if (in_array("cow",$animals)){
echo "Match found";
}else{
echo "Match not found";
}
//array_key_exists():检查给定的键名是否存在于数组中( 参数1:键名 参数2:数组 ):返回bool值
$animals = array("a"=>"dog", "b"=>"cat", "c"=>"cow", "d"=>"horse","d"=>"lion");
echo array_key_exists("a",$animals); //1 没有不返回false值
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
} //The 'first' element is in the array
/* 数组指针操作 */
//key():返回数组内部指针当前指向元素的键名
//current():返回数组当前的元素
//next():把指向当前元素的指针移动到下一个元素的位置,并返回当前元素的值
//prev():把指向当前元素的指针移动到上一个元素的位置,并返回当前元素的值
//end():把当前内部指针指向最后一个元素,并返回该元素的值
//reset():把数组元素指针指向第一个值,并返回这个元素的值
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'
';
}
next($array);
} //fruit1 fruit4 fruit5
/* 遍历数组 */
/*正方向遍历*/
$a = array(10,20,30);
reset($a);
do{
echo key($a)."==>".current($a)."
";
}while(next($a)); // 0==>10 1==>20 2==>30
/*倒向遍历*/
end($a);
do{
echo key($a)."===>".current($a)."
";
}while(prev($a)); //2===>30 1===>20 0===>10
/* 指针 */
$transport = array('foot', 'bike', 'car', 'plane');
/*默认第一个为当前元素*/
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
/*当前元素为'bike'*/
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport); // $mode = 'foot';
$mode = end($transport); // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';
//list():把数组中的值赋给一些变量---------list不是函数
$arr = array("Cat","Dog","Horse","Cow");
list($a,$b,$c,$d) = $arr;
echo $a; //Cat
echo $b; //Dog
echo $c; //Horse
echo $d; //Cow
//array_shift():删除数组中的第一个元素,并返回被删除元素的值
$a = array("1"=>"Dog","2"=>"Cat","3"=>"Horse");
echo array_shift($a); //Dog
print_r ($a); //Array ( [b] => Cat [c] => Horse )
//array_unshift():在数组开关插入一个或多个元素(如果当前数组为索引数组,则从0开始,依此类推;关联数组键名不变)
$a = array("10"=>"Cat","b"=>"Dog",3=>"horse",5=>"lion");
array_unshift($a,"Horse");
print_r($a); //Array ( [0] => Horse [1] => Cat [b] => Dog [2] => horse [3] => lion )
//array_push():向数组最后压入一个或多个元素
$a=array("a"=>"Dog","3"=>"Cat");
array_push($a,"Horse","Bird");
print_r($a); //Array ( [a] => Dog [3] => Cat [4] => Horse [5] => Bird )
//array_pop():删除数组中的最后一个元素
$a=array("Dog","Cat","Horse");
array_pop($a);
print_r($a); //Array ( [0] => Dog [1] => Cat )
/* 数组键值操作 */
//shuffle():将数组打乱,键名为索引数组从0开始(不能直接打印shuffle,分开写)
$animals = array("a"=>"dog", "b"=>"cat", "c"=>"cow", "d"=>"horse","d"=>"lion");
shuffle($animals);
print_r($animals); //Array ( [0] => dog [1] => cow [2] => cat [3] => lion ) 每刷新一次会随机变化
//count():计算数组中的单元数目式对象中的属性个数
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo count($people); //4
//array_flip():返回一个键值反转后的数组
$a = array(0=>"Dog",1=>"Cat",2=>"Horse");
print_r(array_flip($a)); //Array ( [Dog] => 0 [Cat] => 1 [Horse] => 2 )
//array_keys():返回数组所有的键,组成一个数组
$a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a)); //Array ( [0] => a [1] => b [2] => c )
//array_values():返回数组中所有的值,组成一个数组
$a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_values($a)); //Array ( [0] => Horse [1] => Cat [2] => Dog )
//array_reverse():返回一个元素顺序相反的数组
$a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_reverse($a)); //Array ( [c] => Dog [b] => Cat [a] => Horse )
//array_count_values():统计数组中所有值出现的次数
$a = array(1,2,3,4,1,1,3,5,3,2,1,3,4);
print_r(array_count_values($a)); //Array ( [1] => 4 [2] => 2 [3] => 4 [4] => 2 [5] => 1 )
//array_rand():从数组中随机抽取一个或多个元素,注意是键名
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Lion","e"=>"Cow");
print_r(array_rand($a,3)); //Array ( [0] => b [1] => c [2] => e ) ***随机***
//each():返回数组中当前的键/值对并将数组指针向后移动一步
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar); //Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 )
/*每遍历一次,指针向后移动一位*/
$bar = each($foo);
print_r($bar); //Array ( [1] => fred [value] => fred [0] => 1 [key] => 1 )
//array_unique():删除重复值,返回剩余数组
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Dog","e"=>"cow","f"=>"Cow");
print_r(array_unique($a)); //Array ( [a] => Dog [b] => Cat [c] => Horse [e] => cow [f] => Cow )
/* 数组排序 */
/**
* 返回值为1(正值):表示交换顺序
* 返回值为-1(负值):表示不交换顺序
**/
/**
* //原始键名被忽略(从零开始)(字符串顺序)
* sort():对值由小到大
* rsort():对值由大到小
*
* //原始键名保留(字符串顺序)
* asort():对值从小到大
* arsort():对值从大到小
**/
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
sort($my_array);
print_r($my_array); //Array ( [0] => Cat [1] => Dog [2] => Horse )
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
asort($my_array);
print_r($my_array); //Array ( [b] => Cat [a] => Dog [c] => Horse )
/**
* ksort():对下标由小到大
* krsort():对下标由大到小
**/
$my_array = array("h" => "Dog", "s" => "Cat", "a" => "Horse");
ksort($my_array);
print_r($my_array); //Array ( [a] => Horse [h] => Dog [s] => Cat )
$my_array = array("e" => "Dog", "2" => "Cat", "a" => "Horse");//按什么顺序排序
ksort($my_array);
print_r($my_array); //Array ( [a] => Horse [e] => Dog [2] => Cat )
/**
* usort():使用用户自定义的回调函数对数组排序
* uasort():使用用户自定义的回调函数对数组排序并保持索引关联
* uksort():使用用户自定义的回调函数对数组排序对数组键排序
**/
$v = array(1,3,5,2,4);
usort($v,'fun');
function fun($v1,$v2){
return ( $v1 > $v2 ) ? 1 : -1;
}
print_r($v); //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
/* 排序加遍历 */
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a }
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo $key."===>".$value." "; //0===>1 1===>2 2===>3 3===>5 4===>6
}
/* 排序遍历结束 */
/**
* sort():由小到大的字串排序(字母相等,比较不相等之后的一位大小)
* natsort();由小到大的自然排序(字母相等,比较数值)***区分大小写
* natcasesort():不区分大小写的自然排序
**/
$a = array("a" => "id2", "b" => "id12", "c" => "id22","d" => "ID22");
sort($a); print_r($a); //Array ( [0] => ID22 [1] => id12 [2] => id2 [3] => id22 )
natsort($a); print_r($a); //Array ( [0] => ID22 [2] => id2 [1] => id12 [3] => id22 )
natcasesort($a); print_r($a); //Array ( [2] => id2 [1] => id12 [3] => id22 [0] => ID22 )

以上就介绍了php常用函数-数组,包括了php,常用方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage