标签:des style http io ar color os 使用 sp
1、数组的键、值操作函数$arr = [‘rose‘,‘lily‘,‘xiaoxue‘,‘jack‘,‘xiaoming‘,‘jack‘];
/获取一个数组所有的键,以索引数组形式返回
$result =array_keys($arr);
print_r($result);
//获取一个数组所有的值,以索引数组形式返回
$result = array_values($arr);
print_r($result);
// in_array() 可以用于判断上传文件格式是否符合要求。$arr = [‘jpg‘,‘png‘,‘gif‘];
// $result = in_array(‘jacky‘,$arr);
// var_dump($result);
if(in_array(‘jackok‘,$arr)){
echo ‘jack存在于$arr数组中‘;
}else{
echo ‘jack不存在于$arr数组中‘;
}
// 翻转键值
$brr = array_flip($arr);
print_r($brr);
// 翻转数组顺序
$arr = [‘rose‘,‘lily‘,‘xiaoxue‘,‘jack‘,‘xiaoming‘,‘ajack‘];
print_r(array_reverse($arr,true));
$arr = [‘rose‘,‘lily‘,‘xiaoxue‘,‘jack‘,‘xiaoming‘,‘ajack‘];
//返回数组单元个数或者对象成员属性个数
echo count($arr);
<?php
$arr = [‘a‘,‘b‘,‘a‘,‘c‘,‘d‘,‘c‘,‘a‘,‘b‘];
//统计数组单元值出现的次数
$brr = array_count_values($arr);
//移除数组中重复的值
$crr = array_unique($arr);
print_r($crr);
Array
(
[a] => 3
[b] => 2
[c] => 2
[d] => 1
)
Array
(
[0] => a
[1] => b
[3] => c
[5] => d
)
<?php
include ‘db.php‘;
$sql = "select * from bbs_content limit 1";
$data = select($sql)[0];
echo ‘<pre>‘;
//用回调函数过滤数组中的单元
$result = array_filter($data,‘myfunc‘);
function myfunc($x)
{
return !empty($x);
}
print_r($result);
Array
(
[id] => 1
[pid] => 4
[uid] => 1
[title] => 阿斯顿发送到
[content] =>发
[edittime] => 1398212373
[posttime] => 1398212373
[visittimes] => 1
[cost] => 10
[ip] => 2130706433
[isopen] => 1
)
//用回调函数过滤数组中的单元
$result = array_filter($data,‘myfunc‘);
function myfunc($x)
{
return empty($x);
}
print_r($result);
Array
(
[pic] =>
[keywords] =>
[description] =>
[ishot] => 0
[isjinghua] => 0
[istop] => 0
[allowlevel] => 0
[needreply] => 0
[inbox] => 0
)
$arr = [0,1,2,3,true,false,null];
$result = array_filter($arr,‘fun‘);
function fun($a)
{
if($a){
return true;
}else{
return false;
}
}
print_r($result);
$result = array_filter($arr,function($x){ return $x;});
print_r($result);
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 1
)
<?php
$arr = [‘apple‘,‘li‘,‘banana‘,‘xigua‘,‘taozi‘];
echo ‘<pre>‘;
//对数组中的每个成员应用用户函数
array_walk($arr,"myfun");
function myfun(&$v,$k)
{
$v .= ‘lai 2 jin‘;
}
print_r($arr);
array_walk($arr,‘fun‘,‘abc‘);
function fun(&$v,$k,$u)
{
$v.=$u;
}
print_r($arr);
Array
(
[0] => applelai 2 jin
[1] => lilai 2 jin
[2] => bananalai 2 jin
[3] => xigualai 2 jin
[4] => taozilai 2 jin
)
Array
(
[0] => applelai 2 jinabc
[1] => lilai 2 jinabc
[2] => bananalai 2 jinabc
[3] => xigualai 2 jinabc
[4] => taozilai 2 jinabc
)
<?php
$arr = [10,100,20,60,50,60];
$brr = [11,21,31,41,51,61,71];
//将回调函数作用到给定数组的单元上
$result = array_map(‘myfun‘,$arr);
function myfun($x)
{
return $x += 10;
}
echo ‘<pre>‘;
print_r($result);
$result = array_map(‘fun‘,$arr,$brr);
function fun($x,$y)
{
return $x + $y;
}
print_r($result);
Array
(
[0] => 20
[1] => 110
[2] => 30
[3] => 70
[4] => 60
[5] => 70
)
Array
(
[0] => 21
[1] => 121
[2] => 51
[3] => 101
[4] => 101
[5] => 121
[6] => 71
)
<?php
$arr = [10,100,20,60,50,60];
echo ‘<pre>‘;
/*
//对数组排序(升序)
sort($arr);
//对数组排序(降序)
rsort($arr);
print_r($arr);
*/
usort($arr,‘myfun‘);
function myfun($a,$b)
{
retrun $a-$b;
}
print_r($arr);
Array
(
[0] => 10
[1] => 20
[2] => 50
[3] => 60
[4] => 60
[5] => 100
)
<?php
header("content-type:text/html;charset=utf-8");
$users = [‘user1‘ => ‘xiongda‘,‘user2‘ => ‘xiaohei‘,‘user3‘=>‘xibobai‘];
echo ‘<pre>‘;
//对数组进行升序排序并保持索引关系
// asort($users);
//对数组进行降序排序并保持索引关系
// arsort($users);
//使用用户自定义函数,对数组进行排序并保持索引关系
uasort($users,‘myfun‘);
function myfun($a,$b)
{
if($a > $b){
return 1;
}
if($a < $b){
return -1;
}
if($a == $b){
return 0;
}
}
print_r($users);
Array
(
[user2] => xiaohei
[user3] => xibobai
[user1] => xiongda
)
<?php
header("content-type:text/html;charset=utf-8");
$users = [‘user6‘ => ‘xiongda‘,‘user2‘ => ‘xiaohei‘,‘user1‘=>‘xibobai‘];
// 对数组按照键名升序排序
ksort($users);
// 对数组按照键名降序排序
krsort($users);
//使用用户自定义的比较函数对数组中的键名进行排序
uksort($users,‘myfun‘);
function myfun($a,$b)
{
if($a > $b){
return 1;
}
if($a < $b){
return -1;
}
if($a == $b){
return 0;
}
}
echo ‘<pre>‘;
print_r($users);
(
[user1] => xibobai
[user2] => xiaohei
[user6] => xiongda
)
<?php
$arr = [‘image123‘,‘Image102‘,‘image120‘,‘Image1202‘,‘image1231‘];
echo ‘<pre>‘;
sort($arr);
print_r($arr);
echo ‘<hr>‘;
// 自然排序
natsort($arr);
print_r($arr);
echo ‘<hr>‘;
// 不区分大小写自然排序
natcasesort($arr);
print_r($arr);
Array
(
[0] => Image102
[1] => Image1202
[2] => image120
[3] => image123
[4] => image1231
)按照ASCII码排序
Array
(
[0] => Image102
[1] => Image1202
[2] => image120
[3] => image123
[4] => image1231
)按照ASCII码排序
Array
(
[0] => Image102
[2] => image120
[3] => image123
[1] => Image1202
[4] => image1231
)不区分大小写自然排序
<?php//image全是小写i
$arr = [‘image123‘,‘image102‘,‘image120‘,‘image1202‘,‘image1231‘];
echo ‘<pre>‘;
sort($arr);
print_r($arr);
echo ‘<hr>‘;
// 自然排序
natsort($arr);
print_r($arr);
Array
(
[0] => image102
[1] => image120
[2] => image1202
[3] => image123
[4] => image1231
)
Array
(
[0] => image102
[1] => image120
[3] => image123
[2] => image1202
[4] => image1231
)
<?php
include ‘db.php‘;
$sql = ‘select regtime,username,money,email from bbs_user limit 3‘;
$data = select($sql);
echo ‘<pre>‘;
// print_r($data);
foreach($data as $v){
$email[] = $v[‘email‘];
$username[] = $v[‘username‘];
$regtime[]=$v[‘regtime‘];
}
// print_r($email);
// print_r($username);
//多维数组排序
array_multisort($username,SORT_DESC,$regtime,SORT_ASC,$email,SORT_ASC,$data);
print_r($data);
Array
(
[0] => Array
(
[regtime] => 1397632974
[username] => jackyok
[money] => 100
[email] => jads@qq.com
)
[1] => Array
(
[regtime] => 1397632773
[username] => jack
[money] => 100
[email] => 50377203@qq.com
)
[2] => Array
(
[regtime] => 1397633371
[username] => alibaba
[money] => 100
[email] => 50377203@qq.com
)
)
<?php
$arr = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘];
// $result = array_slice($arr,3);
$result = array_slice($arr,-4,-1);
var_dump($result);
结果:array(3) { [0]=> string(1) "c" [1]=> string(1) "d" [2]=> string(1) "e" }
<?php
$arr = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘];
// $brr = array_splice($arr,1,1,‘x‘);
// $brr = array_splice($arr,1);
// $brr = array_splice($arr,-1);
// $brr = array_splice($arr,1,1);
// $brr = array_splice($arr,1,-1);
// $brr = array_splice($arr,-3,1);
// $brr = array_splice($arr,-3,-1,[‘x‘,‘y‘]);
$brr = array_splice($arr,-3,-1,array(array(‘a‘,‘b‘)));
echo ‘<pre>‘;
print_r($arr);echo ‘<hr>‘;
print_r($brr);
结果:Array( [0] => a [1] => b [2] => c [3] => Array ( [0] => a [1] => b ) [4] => f)
Array( [0] => d [1] => e)
<?php
$arr = [‘a‘,‘b‘,‘c‘,‘d‘];
$brr = [‘huanggua‘,‘mangguo‘,‘taozi‘,‘boluo‘];
// 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
$crr = array_combine($arr,$brr);
echo ‘<pre>‘;
print_r($crr);结果:Array( [a] => huanggua [b] => mangguo [c] => taozi [d] => boluo)
<?php
$arr = array(‘xiaorui‘,10=>‘enzhu‘,‘jingjing‘,‘user1‘=>‘minghui‘,‘caojie‘);
$brr = array(‘zhangtao‘,‘hongli‘,10=>‘fengtao‘,‘henian‘,‘user1‘=>‘haijing‘);
//合并一个或多个数组,键名同名的后面的会把前边的覆盖掉
$crr = array_merge($arr,$brr);
echo ‘<pre>‘;
print_r($crr);结果:Array( [0] => xiaorui [1] => enzhu [2] => jingjing [user1] => haijing [3] => caojie [4] => zhangtao [5] => hongli [6] => fengtao [7] => henian)
<?php
$arr = array(‘xiaorui‘,‘enzhu‘,‘jingjing‘,‘haijing‘,‘caojie‘);
$brr = array(‘zhangtao‘,‘enzhu‘,‘fengtao‘,‘henian‘,‘haijing‘);
//获取多个数组交集
// $crr = array_intersect($arr,$brr);
//获取多个数组差集
$crr = array_diff($arr,$brr);
echo ‘<pre>‘;
print_r($crr);
<?php
$brr = array(‘zhangtao‘,‘enzhu‘,‘fengtao‘,‘henian‘,‘haijing‘);
//将数组最后一个单元弹出
// $result = array_pop($brr);
//将一个或多个单元压入数组的末尾(入栈)
// $result = array_push($brr,‘shenchao‘);
//将数组开头的单元移出数组
// $result = array_shift($brr);
//在数组开头插入一个或多个单元
// $result = array_unshift($brr,array(‘cenghao‘,‘gaolaoshi‘));
echo ‘<pre>‘;
print_r($brr);
print_r($result);
<?php
$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
// 从数组中随机取出一个或多个单元
$keys = array_rand($arr,3);
print_r($keys);
foreach($keys as $v){
echo $arr[$v].‘<br>‘;
}
<?php
$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
//shuffle — 将数组打乱
shuffle($arr);
print_r($arr);
echo $arr[0].‘<br>‘;
echo $arr[1].‘<br>‘;
echo $arr[2];
//建立一个包含指定范围单元的数组
//第一个参数开始位置(最小值) 第二个参数结束位(最大值) 第三个参数步长
$brr = range(1,54,3);
echo ‘<pre>‘;
print_r($brr);
标签:des style http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/lxw89/p/0dd0d83c-b6f3-4dfe-b4fe-4742204d7193.html