array_values()
返回数组中所有的值并建立数字索引
$lamp = array('os' =>"Linux","webserver"=>"Apache","db"=>"MySql","language" =>"PHP","full"=>null );
list($os,$webserver,$db,$language) = array_values($lamp);
echo $os."<br>"; // Linux
echo $webserver."<br>"; // Apache
echo $db."<br>"; // MySql
echo $language."<br>"; // PHP
array_keys()
返回数组中所有的键名
$keys = array_keys($lamp);
print_r($keys);
//Array ( [0] => os [1] => webserver [2] => db [3] => language )
in_array()
判断值是否在数组中,区分大小写,第三个参数BOOL值判断类型是否相同
if (in_array('linux', $lamp)) {
echo "存在";
}
与isset区别,假如值为null的话,会判定不存在,而in_array会判定存在
if (isset($lamp['full'])) {
echo "存在";
}
array_search
在数组中搜索给定的值,如果成功返回相应的键名
$wp = ["wp",1,2,3,4,-4,-3,false,0,null];
function demo($value){
if ($value>0) {
return true;
} else{
return false;
}
}
// 过滤函数,回调函数及匿名函数的形式
var_dump(array_filter($wp,"demo"));
var_dump(array_filter($wp,function($value){
return $value%2 == 0;
}));
//array_walk,如果需要操作对原值的改变,需要加个&,array_walk本身返回的是BOOL值
function myfun(&$value){
$value = $value * $value;
}
print_r($wp);
echo "<br>";
array_walk($wp, "myfun");
print_r($wp);
echo "<br>";
//对键和值处理
array_walk($wp,function($value,$key){
echo "{$key}====>{$value}";
});
//多个参数的处理
array_walk($wp, function($value,$key){
echo "{key}{str}{value}";
},"######");
$wps = [1,3,2,5,4,6,8,7];
//冒泡排序
$len = count($wps);
for ($j=0; $j <$len-1 ; $j++) {
for ($i=0; $i < $len- i -1; $i++) {
if ($wps[$i]< $wps[$i+1]) {
$tmp = $wps[$i];
$wps[$i] = $wps[$i+1];
$wps[$i+1] = $tmp;
}
}
}
网友评论