php数组遍历类与用法示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php数组遍历类与用法示例相关的知识,希望对你有一定的参考价值。

参考技术A 本文实例讲述了php数组遍历类与用法。分享给大家供大家参考,具体如下:
<?php
class
scanArray
public
$arr;
public
$where;
private
$str;
public
function
scan($arr,$where="array")
$this->arr
=
$arr;
$this->where
=
$where;
foreach($this->arr
as
$k=>$v)
if(is_array($v))
$this->where
=
($this->where)."[$k]";
$this->scan($v,$this->where);
else
$this->str
.=
$this->where."[$k]=".$v.'<br
/>';


return
$this->str;

function
__destruct()
unset($this->arr);
unset($this->where);


$a
=
array('g'=>"a",'vv'=>array("b"=>"b","l"=>"c","xx"=>array("e","g")));
$ah
=
new
scanArray();
$b
=
$ah->scan($a);
echo
$b;
运行结果:
array[g]=a
array[vv][b]=b
array[vv][l]=c
array[vv][xx][0]=e
array[vv][xx][1]=g
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》及《PHP常用遍历算法与技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:PHP遍历数组的方法汇总PHP
数组遍历方法大全(foreach,list,each)PHP
数组遍历foreach语法结构及实例PHP中多维数组的foreach遍历示例php实现遍历多维数组的方法PHP中使用foreach()遍历二维数组的简单实例PHP遍历数组的三种方法及效率对比分析PHP实现的操作数组类库定义与用法示例PHP数组操作类实例PHP数组生成XML格式数据的封装类实例

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数组遍历类与用法示例的主要内容,如果未能解决你的问题,请参考以下文章

PHP中遍历数组有三种常用的方法

PHP获取两个多维数组的差集

PHP获取两个多维数组的差集

foreach用法

PHP连接和拆分数组array_combine()和array_slice()用法示例

PHP中常用数组的一些基本用法