标签:bad 产生 merge first call reduce john cti rds
echo ‘1、array_multisort( )排序<br/>‘;
$arr = array(5,6,2,7,8,1);
array_multisort($arr,SORT_DESC);
print_r($arr);
echo ‘<hr/>‘;
echo ‘2、array_merge( )合并数组<br/>‘;
$arr = array(1,3,54,7,‘rrr‘,‘ryuih‘,‘end‘);
$c = array();
print_r(array_merge($arr,$c));
echo ‘<hr/>‘;
$a=array(
array(1,3,54,7,‘rrr‘,‘ryuih‘,‘end‘),
array(3=>"red",4=>"green"),
);
print_r(merge_array($a));
function merge_array($a){
return call_user_func_array(‘array_merge‘,$a);
}
echo ‘<hr/>‘;
echo ‘3、array_slice( )分页<br/>‘;
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown");
print_r(array_slice($a,0,2));
echo ‘<hr/>‘;
echo ‘4、array_filter( )过滤数组为空的值<br/>‘;
$array = array(
0 => ‘霜天部落‘,
1 => false,
2 => 1,
3 => null,
4 => ‘‘,
5 => ‘http://www.hzhuti.com‘,
6 => ‘0‘,
7 => ""
);
print_r(array_filter($array));
echo ‘<br/>‘;
echo ‘回调函数(PHP 4 >= 4.0.6, PHP 5, PHP 7)<br/>‘;
$var;
/**
* @param $var
* @return int
*/
function odd($var)
{
if($var % 2 == 1){
return 1;
}else{
return 0;
}
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
print_r(array_filter($array1, "odd"));
echo ‘<hr/>‘;
echo ‘5、检测是否是序列化<br/>‘;
$ser = serialize(array(‘积极‘,‘hkdjh‘,‘积极饿‘));
echo is_serialized($ser);
function is_serialized( $data ) {
$data = trim( $data );
if ( ‘N;‘ == $data )
return true;
if ( !preg_match( ‘/^([adObis]):/‘, $data, $badions ) )
return false;
switch ( $badions[1] ) {
case ‘a‘ :
case ‘O‘ :
case ‘s‘ :
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
return true;
break;
case ‘b‘ :
case ‘i‘ :
case ‘d‘ :
if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
return true;
break;
}
return false;
}
echo ‘<hr/>‘;
echo ‘6、array_map( ) 使用用户自定义函数来改变数组的值 <br/>‘;
function myfunction($v)
{
if ($v==="Dog")
{
return "Fido";
}
return $v;
}
$a=array("Horse","Dog","Cat");
print_r(array_map("myfunction",$a));
echo ‘<br/>‘;
echo ‘array_map( ) 为null 合并数组 <br/>‘;
$a1=array("Dog","Cat");
$a2=array("Puppy","Kitten");
print_r(array_map(null,$a1,$a2));
echo ‘<br/>‘;
$array = array(‘AAA‘, ‘bbb‘);
$map_array = array_map(‘strtolower‘, $array);
print_r($map_array);
print_r($array);
echo ‘<br/>‘;
echo ‘如果回调函数需要直接作用于数组中的值,可以将回调函数的第一个参数指定为引用:&$value<br/>‘;
function array_walk_func(&$v, $k){
$v = strtolower($v);
}
$walk_array = array_walk($array, ‘array_walk_func‘);
print_r($walk_array);
print_r($array);
echo ‘<hr/>‘;
echo ‘7、array_column( )快速实现:将二维数组转为一维数组(PHP 5 >= 5.5.0, PHP 7)<br/>‘;
if( ! function_exists(‘array_column‘))
{
function array_column($input, $columnKey, $indexKey = NULL)
{
$columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE;
$indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE;
$indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE;
$result = array();
foreach ((array)$input AS $key => $row)
{
if ($columnKeyIsNumber)
{
$tmp = array_slice($row, $columnKey, 1);
$tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL;
}
else
{
$tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL;
}
if ( ! $indexKeyIsNull)
{
if ($indexKeyIsNumber)
{
$key = array_slice($row, $indexKey, 1);
$key = (is_array($key) && ! empty($key)) ? current($key) : NULL;
$key = is_null($key) ? 0 : $key;
}
else
{
$key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
}
}
$result[$key] = $tmp;
}
return $result;
}
}
$records = array(
array(
‘id‘ => 2135,
‘first_name‘ => ‘John‘,
‘last_name‘ => ‘Doe‘,
),
array(
‘id‘ => 3245,
‘first_name‘ => ‘Sally‘,
‘last_name‘ => ‘Smith‘,
),
array(
‘id‘ => 5342,
‘first_name‘ => ‘Jane‘,
‘last_name‘ => ‘Jones‘,
),
array(
‘id‘ => 5623,
‘first_name‘ => ‘Peter‘,
‘last_name‘ => ‘Doe‘,
)
);
$first_names = array_column($records, ‘first_name‘);
print_r($first_names);
echo ‘<br/>‘;
$last_names = array_column($records, ‘last_name‘, ‘id‘);
print_r($last_names);
echo ‘<hr/>‘;
echo ‘8、array_reduce( $arr , callable $callback ) 使用回调函数迭代地将数组简化为单一的值。<br/>
其中$arr 为输入数组,$callback($result , $value)接受两个参数,$result为上一次迭代产生的值,$value是当前迭代的值。<br/>‘;
$arr = array(‘1‘,‘2‘,‘3‘); //计算数组中数字的和
$sum = 0;
foreach($arr as $v){ //使用 foreach循环计算
$sum += $v;// echo $sum
}
echo $sum.‘<br/>‘;
echo array_reduce($arr , function($result , $v){ //使用array_reduce()迭代求和
Return $result+$v;
});
标签:bad 产生 merge first call reduce john cti rds
原文地址:http://www.cnblogs.com/hanling/p/7078065.html