您当前的位置:首页 > 互联网教程

php多维数组foreach取值

发布时间:2025-05-21 18:59:55    发布人:远客网络

php多维数组foreach取值

一、php多维数组foreach取值

本人写了个函数来查找记录,稍微做了通用性扩展,凑合着用吧,下面为测试代码:

array('id'=> 1,'edition'=>'a'),

array('id'=> 2,'edition'=>'b'),

array('id'=> 3,'edition'=>'c'),

array('id'=> 4,'edition'=>'d'),

array('id'=> 5,'edition'=>'e'),

array('id'=> 6,'edition'=>'f'),

array('id'=> 7,'edition'=>'g'),

array('id'=> 8,'edition'=>'h'),

array('id'=> 9,'edition'=>'i'),

array('id'=> 10,'edition'=>'j'),

array('id'=> 11,'edition'=>'k'),

array('id'=> 12,'edition'=>'l'),

array('id'=> 13,'edition'=>'m'),

array('id'=> 14,'edition'=>'n'),

array('id'=> 15,'edition'=>'o'),

array('id'=> 16,'edition'=>'p'),

array('id'=> 17,'edition'=>'q'),

array('id'=> 18,'edition'=>'r'),

array('id'=> 19,'edition'=>'s'),

array('id'=> 20,'edition'=>'t')

*返回相邻N条记录,若返回空数组则说明没有找到该记录

*$records为记录数组,$id为要查找的记录,$num为要返回的记录数,默认为10

function getRecords($records,$id,$num=10){

$arrayRet=array();//返回数组初始化

if($num<1||!is_array($records)){//参数有问题

$records=array_values($records);

$len=count($records);//记录个数

$pos=NULL;//存储查找到的记录位置

foreach($records as$k=>$v){

if(isset($v['id'])&&$v['id']==$id){

if(is_int($pos)){//查找到了记录

while($total<$num&&$low>=$limit){

array_unshift($arrayRet,$records[$low]);

while($total<$num&&$high<=$limit){

array_push($arrayRet,$records[$high]);

while($total<$num&&$low>=$limit){

array_unshift($arrayRet,$records[$low]);

echo"查找id为12的记录,返回10条:\n";

print_r(getRecords($a,12));//查找id为12的记录,返回10条

echo"\n\n查找id为3的记录,返回5条\n";

print_r(getRecords($a,3,5));//查找id为3的记录,返回5条

echo"\n\n查找id为293的记录,返回15条\n";

print_r(getRecords($a,293,15));//查找id为293的记录,返回15条

二、php中数组怎么循环输出

$g_id= isset($_GET['id'])?$_GET['id']:'1';//定义变量$g_id,使用三元运算符是为了避免出现waring

$p_id=($g_id<=9)?('0'.$g_id):($g_id);//定义变量$p_id

//定义一组URL,这里我们用一组图片的地址,可以看到图像文件URL会根据浏览器参数id的不同而得到不同的URL

'0'=> array('uri'=>'/images/p'.$p_id.'_01.jpg'),

'1'=> array('uri'=>'/images/p'.$p_id.'_02.jpg'),

'2'=> array('uri'=>'/images/p'.$p_id.'_03.jpg'),

'3'=> array('uri'=>'/images/p'.$p_id.'_04.jpg'),

'4'=> array('uri'=>'/images/p'.$p_id.'_05.jpg'),

'5'=> array('uri'=>'/images/p'.$p_id.'_06.jpg'),

'6'=> array('uri'=>'/images/p'.$p_id.'_07.jpg'),

'7'=> array('uri'=>'/images/p'.$p_id.'_08.jpg'),

'8'=> array('uri'=>'/images/p'.$p_id.'_09.jpg'),

'9'=> array('uri'=>'/images/p'.$p_id.'_10.jpg'),

'10'=>array('uri'=>'/images/p'.$p_id.'_11.jpg'),

'11'=>array('uri'=>'/images/p'.$p_id.'_12.jpg'),

'12'=>array('uri'=>'/images/p'.$p_id.'_13.jpg')

//使用foreach循环输出img标签,使得网页能正常显示这些图片

if(file_exists("..".$v['uri'])){//这里需要判断一下图片文件是否存在,如果不存在,则不进行输出,否则页面上会有一个img加载失败的警告信息

echo'<img src="'.$v['uri'].'"/><br/>';

下面我们来看看for、foreach、while这三种循环方式的写法

for($i=0;$i<count($Uri);$i++){

echo$Uri[$i]['uri']."<br/>";

echo$v['uri']."<br/>";

echo$Uri[$i]['uri']."<br/>";

三、php编程输出100以内的素数

用函数实现1-100中素数的算法如下:

<?php//定义函数function sushu(){//求100以内质数

编写Python程序求1000以内的质数

if(n%i==0)i在2到n-1之间任取一个数,如果n能被整除则不是素数,否则就是素数

if(n%i==0)/*i在2到n/2之间任取一个数,如果n能被整除则不是素数,否则就是素数

3、for(i=2;i<(n=sqrt(n));i++)

if(n%i==0)/*i在2到sqrt(n)之间任取一个数,如果n能被整除则不是素数,否则就是素数,在下省了下面的输出步骤*/

参考资料来源:百度百科-PHP(超文本预处理器)