PHP数组

php

浏览数:112

2019-1-7

//数组遍历
foreach()
用法:foreach(数组 as 值)
 
-------------------------------------------
list()
 
用法:$arr=array(1,2,3,4);
     list($a,$b,$c,$d)=arr;
     $a=1;
     $b=2;
     $c=3;
     $d=4.
-------------------------------------------
each()
用法:each($arr)
 
$arr=array("a"=>"aaa","b"=>"bbb","c"=>"ccc","d"=>"ddd");
for($i=0;$i<4;$i++){
    print_r(each($arr));
    echo "<br>";
}
 
Array ( [1] => aaa [value] => aaa [0] => a [key] => a ) 
Array ( [1] => bbb [value] => bbb [0] => b [key] => b ) 
Array ( [1] => ccc [value] => ccc [0] => c [key] => c ) 
Array ( [1] => ddd [value] => ddd [0] => d [key] => d ) 
-------------------------------------------
explode()炸开将字符串依照某个特定符号分割成数组
用法:explode(分隔符,数组)
 
$str="java*php*html*css";
print_r(explode("*",$str));
 
Array ( [0] => java [1] => php [2] => html [3] => css )
-------------------------------------------
implode()将数组合并为字符串
用法:implode("指定合并符号",数组)
 
$arr=array("a"=>"aaa","b"=>"bbb","c"=>"ccc","d"=>"ddd");
echo implode("@",$arr);
aaa@bbb@ccc@ddd
-------------------------------------------
count()计算数组的元素个数
用法:count(数组,默认0/如果设为1将对多维数组递归求元素总数)
 
$arr=array("a"=>"aaa","b"=>"bbb","c"=>"ccc","d"=>"ddd");
echo count($arr);
4
 
$arr=array("书籍"=>array('文学','历史','地理'),"体育用品"=>array("篮球","足球"),"水果"=>array('苹果'));
echo count($arr,1)
9
-------------------------------------------
array_pop()该函数删除数组的最后一项,并且返回删除的元素值
用法:array_pop(数组)
 
$arr=array("a","b","c","d");
echo array_pop($arr)."<br>";
print_r($arr);
 
d
Array ( [0] => a [1] => b [2] => c )
-------------------------------------------
array_search()用来搜索某个值是否在数组中,在则返回该值得键,否则返回false
用法:array_search(元素值,数组)
 
$arr=array("a","b","c","d");
$a="a";
echo array_search($a,$arr);
 
0
-------------------------------------------
array_push()向数组中添加元素到数组尾部
用法:array_push(数组,元素1,元素2...)
 
$arr=array("a","b","c","d");
array_push($arr,"e","f","g");
print_r($arr);
 
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g )
-------------------------------------------
array_unique()删除数组中重复的元素,返回去重后的数组,原数组不变
用法:array_unique(数组)
 
$arr=array("a","b","c","d","a","d","g");
print_r(array_unique($arr));
echo "<br>";
print_r($arr);
 
Array ( [0] => a [1] => b [2] => c [3] => d [6] => g ) 
Array ( [0] => a [1] => b [2] => c [3] => d [4] => a [5] => d [6] => g )
-------------------------------------------