因为小程序学习中经常会用到数组操作,为了备忘,把一些基本的操作进行汇总(持续更新备忘中)
首先来PHP部分
数组转字符串
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
echo implode(',',$arr1); //这个是通过指定字符来将数组中各个值连接成字符串,英文逗号可换成其他的
?>
输出结果为 a1,b2,c3,d4,e5"
字符串转数组
<?php
$str2 = 'a1,b2,c3,d4,e5';
print_r(explode(',',$str2)); //这个是通过字符串中特定字符,来分隔成数组,英文逗号可更改成其他的
?>
输出结果为:
Array
(
[0] => a1
[1] => b2
[2] => c3
[3] => d4
[4] => e5
)
数组合并
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
$arr2 = array('aa','bb','cc','dd','ee');
print_r(array_combine($arr1,$arr2)); //放在前面的数组中的值作为key,后面的值最为value
?>
输出结果为:
Array
(
[a1] => aa
[b2] => bb
[c3] => cc
[d4] => dd
[e5] => ee
)
将数组中的key和value分成2个数组
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
print_r(array_keys($arr1)); //这个是将数组中的key合并成新数组
print_r(array_values($arr1)); //这个是将数组中的value合并成新数组
?>
输出结果为:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
)
Array
(
[0] => a1
[1] => b2
[2] => c3
[3] => d4
[4] => e5
)
数组中查询
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
echo array_search('b2',$arr1); //查询数组中value为b2对应的key值
?>
输出结果为:
b
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
print_r(array_splice($arr1,2,1)); //这个是将数组中某个元素取出来,原数组也会改变,这个也可以用来操作数组删除数据用
print_r(($arr1));
?>
输出结果为:
Array
(
[c] => c3
)
原数组会把key为c的去除掉,
Array
(
[a] => a1
[b] => b2
[d] => d4
[e] => e5
)
判断数组中是否存在某个key或者value
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
var_dump(isset($arr1['b2'])); //判断数组中有没有下标是b2的元素,实际没有,所以返回false
var_dump(isset($arr1['b'])); //判断数组中有没有下标是b的元素,实际有,所以返回true
var_dump(in_array('a1',$arr1)); //判断数组中有没有包含值为a1的元素,实际有,所以返回true
?>
输出结果为:
bool(false)
bool(true)
bool(true)
往数组中添加元素
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
$arr1['f'] = 'f6'; //往数组中添加key为f,value为f6的数据,也可通过这个直接改变key为f的元素对应的value
$arr1[] = 'g7'; //依次往下添加,如果原来的key不是默认的0开始的数字,那这样添加的第一个为key=0,然后依次+1
array_push($arr1, 'h8','i9'); //这个是往数组中push数据,同上,key为数字
?>
输出结果为:
Array
(
[a] => a1
[b] => b2
[c] => c3
[d] => d4
[e] => e5
[f] => f6
[0] => g7
[1] => h8
[2] => i9
)
数组排序
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
krsort($arr1); //按key值降序排列数组,升序的话,就是ksort($arr1)
rsort($arr1); //按value值降序排列数组,升序的话,就是sort($arr1), 这个会把原来的key去除,重新用数字编号
?>
输出结果为:
Array
(
[f] => f6
[e] => e5
[d] => d4
[c] => c3
[b] => b2
[a] => a1
[0] => g7
)
Array
(
[0] => g7
[1] => f6
[2] => e5
[3] => d4
[4] => c3
[5] => b2
[6] => a1
)
多维数组排序
<?php
$arr =[
array (
'name' => 'sham' ,
'age' =>28
),
array (
'name' => 'sham2' ,
'age' =>14
),
array (
'name' => 'sham3' ,
'age' =>59
),
array (
'name' => 'sham4' ,
'age' =>23
),
array (
'name' => 'sham5' ,
'age' =>23
),
array (
'name' => 'sham6' ,
'age' =>21
),
];
array_multisort (array_column( $arr , 'age' ),SORT_DESC, $arr );
?>
输出结果为:
Array
(
[0] => Array
(
[name] => sham3
[age] => 59
)
[1] => Array
(
[name] => sham
[age] => 28
)
[2] => Array
(
[name] => sham4
[age] => 23
)
[3] => Array
(
[name] => sham5
[age] => 23
)
[4] => Array
(
[name] => sham6
[age] => 21
)
[5] => Array
(
[name] => sham2
[age] => 14
)
)
数组指针指向
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
echo next($arr1); //输出当前指针指向的下一个key的value,第一次使用是时是获取第二个元素值
echo current($arr1); //输出当前指针所在的key的value
echo prev($arr1); //输出当前指针指向的上一个的key的value
echo end($arr1); //输出数组最后一个key的value
echo current($arr1);
echo reset($arr1); //重置指针,回复到指向第一个key的value
echo current($arr1);
?>
输出结果为:
b2 //对应next()
b2 //对应current()
a1 //对应prev()
g7 //对应end()
g7
a1 //对应reset()
a1"
数组比较
<?php
$arr1 = array('a'=>'a1','b'=>'b2','c'=>'c3','d'=>'d4','e'=>'e5');
$arr2 = array('a','b2','cc','d4','f6');
print_r(array_diff($arr1,$arr2)); //只比较value,如第一个数组中的value,不存在与第二个数组,则放入新的数组后返回
print_r(array_diff_assoc($arr1,$arr2)); //比较key和value,将第一个数组中key和value与第二个一一比较,不匹配的,则放入新数组后返回
print_r(array_diff_key($arr1,$arr2)); //只比较key,第一个数组中的key,如不存在与第二个,则放入新数组后返回
?>
输出结果为:
array_diff()
Array
(
[a] => a1
[c] => c3
[e] => e5
[0] => g7
)
array_diff_assoc()
Array
(
[a] => a1
[b] => b2
[c] => c3
[d] => d4
[e] => e5
[f] => f6
[0] => g7
)
array_diff_key()
Array
(
[a] => a1
[b] => b2
[c] => c3
[d] => d4
[e] => e5
[f] => f6
)
?一个在前往码农道路上走走停停的行政文员
|