PHP如何高效地对根据键值对数组元素进行归类
?phpfunction filter($elem){ return $elem['type'] == '空调';}$arr=Array( Array('id' = 1,'type' = '空调'), Array('id' = 2,'type' = '电视机'), Array('id' = 3,'type' = '空调'));$newarr = array_filter($arr, 'filter');print_r($newarr);exit;
创新互联-专业网站定制、快速模板网站建设、高性价比汉阴网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式汉阴网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖汉阴地区。费用合理售后完善,10多年实体公司更值得信赖。
如何实现PHP根据数组的值进行分组
PHP根据数组的值分组,php array中没有自带这个函数但是很常用
代码:
$_array = array(
array(1,11,'2016-05-18'),
array(2,11,'2016-05-18'),
array(3,22,'2016-05-18'),
array(4,22,'2016-05-18'),
array(5,33,'2016-05-19'),
array(6,33,'2016-05-19'),
array(7,44,'2016-05-19'),
array(8,44,'2016-05-19'),
array(9,55,'2016-05-20'),
array(10,55,'2016-05-20'),
);
var_dump(array_val_chunk($_array));
function array_val_chunk($array){
$result = array();
foreach ($array as $key = $value) {
$result[$value[1].$value[2]][] = $value;
}
$ret = array();
//这里把简直转成了数字的,方便同意处理
foreach ($result as $key = $value) {
array_push($ret, $value);
}
return $ret;
}
运行结果如下:
array(5) {
[0]=
array(2) {
[0]=
array(3) {
[0]=
int(1)
[1]=
int(11)
[2]=
string(10) "2016-05-18"
}
[1]=
array(3) {
[0]=
int(2)
[1]=
int(11)
[2]=
string(10) "2016-05-18"
}
}
[1]=
array(2) {
[0]=
array(3) {
[0]=
int(3)
[1]=
int(22)
[2]=
string(10) "2016-05-18"
}
[1]=
array(3) {
[0]=
int(4)
[1]=
int(22)
[2]=
string(10) "2016-05-18"
}
}
[2]=
array(2) {
[0]=
array(3) {
[0]=
int(5)
[1]=
int(33)
[2]=
string(10) "2016-05-19"
}
[1]=
array(3) {
[0]=
int(6)
[1]=
int(33)
[2]=
string(10) "2016-05-19"
}
}
[3]=
array(2) {
[0]=
array(3) {
[0]=
int(7)
[1]=
int(44)
[2]=
string(10) "2016-05-19"
}
[1]=
array(3) {
[0]=
int(8)
[1]=
int(44)
[2]=
string(10) "2016-05-19"
}
}
[4]=
array(2) {
[0]=
array(3) {
[0]=
int(9)
[1]=
int(55)
[2]=
string(10) "2016-05-20"
}
[1]=
array(3) {
[0]=
int(10)
[1]=
int(55)
[2]=
string(10) "2016-05-20"
}
}
}
php 分割数组数据
str_split函数能实现你这样的功能,把字符串按长度分割为数组,例如:
?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?
代码输出的结果如下:
Array
(
[0] = H
[1] = e
[2] = l
[3] = l
[4] = o
[5] =
[6] = F
[7] = r
[8] = i
[9] = e
[10] = n
[11] = d
)
Array
(
[0] = Hel
[1] = lo
[2] = Fri
[3] = end
)
php 把数组按条件分成不同的数组
$arr1 = array("In/4833895_0001.jpg","In/4834113_0002.jpg","Out/4834223_0001.jpg","In/4834301_0003.jpg","Out/4834442_0002.jpg","In/4834520_0004.jpg","Out/4834598_0003.jpg","In/4834723_0005.jpg","Out/4834817_0004.jpg","In/4834863_0006.jpg","Out/4835004_0005.jpg","In/4835067_0007.jpg","Out/4835254_0006.jpg","Out/4835410_0007.jpg","Out/04835629_0008.jpg","Out/4835785_0009.jpg");
foreach($arr1 as $k = $val){
if(strpos($val,"In")===0){
$inArr[] = $val;
}else{
$outArr[] = $val;
}
}
print_r($inArr);
echo "br";
print_r($outArr);
Array
(
[0] = In/4833895_0001.jpg
[1] = In/4834113_0002.jpg
[2] = In/4834301_0003.jpg
[3] = In/4834520_0004.jpg
[4] = In/4834723_0005.jpg
[5] = In/4834863_0006.jpg
[6] = In/4835067_0007.jpg
)
Array
(
[0] = Out/4834223_0001.jpg
[1] = Out/4834442_0002.jpg
[2] = Out/4834598_0003.jpg
[3] = Out/4834817_0004.jpg
[4] = Out/4835004_0005.jpg
[5] = Out/4835254_0006.jpg
[6] = Out/4835410_0007.jpg
[7] = Out/04835629_0008.jpg
[8] = Out/4835785_0009.jpg
)
PHP根据二维数组元素数量拆分成若干个小数组?
php数组分组可以使用函数array_chunk,按照给定的规则进行遍历,如果数组的num值大于8,就整除8,获取整除的数据,然后按照整除数进行分组分块即可。
文章题目:php数组内的数据分类,php中数组的定义有几种
分享URL:http://scyingshan.cn/article/hohiih.html