PHP中常用数组的一些基本用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP中常用数组的一些基本用法相关的知识,希望对你有一定的参考价值。
php中常用数组的一些基本用法和操作:
索引数组
$arr=array("aa","ss","dd","ff");
echo $arr[0].‘, ‘.$arr[1].‘ ,‘ .$arr[2].‘ ,‘ .$arr[3];
获取数组的长度
echo count($arr);
遍历索引数组
for($i=0;$i<count($arr);$i++)
{
echo $arr[$i];
echo "\n";
}
foreach ($arr as $item)
{
echo $item;
echo "\n";
}
关联数组
$arr=array("a"=>"A","s"=>"S","q"=>"Q");
foreach ($arr as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
数组排序
以升序多数组排序
$arr=array("6","2","1","7","5");
sort($arr);
foreach ($arr as $item)
{
echo $item;
echo "\n";
}
以降序排序
rsort($arr);
foreach ($arr as $item)
{
echo $item;
echo "\n";
}
根据值对关联数组进行升序排序
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
asort($age);
foreach ($age as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
根据值对关联数组进行降序排序
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
arsort($age);
foreach ($age as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
根据键对关联数组进行升序排序
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
ksort($age);
foreach ($age as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
根据键对关联数组进行降序排序
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
krsort($age);
foreach ($age as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
获取关联数组中所有的值
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
$arr=array_values($age);
foreach ($arr as $item)
{
echo $item;
echo "\n";
}
获取关联数组中所有的键名
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
$arr=array_keys($age);
foreach ($arr as $item)
{
echo $item;
echo "\n";
}
检查数组中是否存在某个值
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
echo in_array(‘9‘,$age);
交换数组中的键和值
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
$newage=array_flip($age);
foreach ($newage as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
返回一个单元数组相反的数组
$age=array("a"=>"11","s"=>"9","r"=>"13","d"=>"8");
$newage=array_reverse($age);
foreach ($newage as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
计算数组中所有的值出现的次数
$age=array("a"=>"11","s"=>"9","r"=>"9","d"=>"8");
$num=array_count_values($age);
foreach ($num as $item)
{
echo $item.‘ ‘;
}
移除数组中重复的值,只保留一个
$age=array("a"=>"11","s"=>"9","r"=>"9","d"=>"8");
$newage=array_unique($age);
foreach ($newage as $key=>$item)
{
echo $key.‘=‘.$item;
echo "\n";
}
合并数组
$arr1=array("1","2","3","4");
$arr2=array("2","3","5","1");
$arr=array_merge($arr1,$arr2);
foreach ($arr as $item)
{
echo $item.‘,‘;
}
计算数组的交集
$arr1=array("1","2","3","4");
$arr2=array("2","3","5","1");
$arr=array_intersect($arr1,$arr2);
foreach ($arr as $item)
{
echo $item.‘,‘;
}
计算数组的差
$arr1=array("1","2","3","4");
$arr2=array("2","3","5","1");
$arr=array_diff($arr2,$arr1);
foreach ($arr as $item)
{
echo $item.‘,‘;
}
从数组中随机取出一个或多个元素
$arr=array("1","a","3","e","e","4","e");
$newarr=array_rand($arr);
echo $newarr;
echo "\n";
$arr1=array_rand($arr,3);
foreach ($arr1 as $item)
{
echo $item.‘,‘;
}
将数组打乱
$arr=array("1","2","3","4","5","6","7");
shuffle($arr);
print_r($arr);
foreach ($arr as $item)
{
echo $item.‘,‘;
}
取整,去掉小数点之后的值
echo floor(15.4);
以上是关于PHP中常用数组的一些基本用法的主要内容,如果未能解决你的问题,请参考以下文章