PHP一个数组以标签为单位循环

比如下面的表单

1、这里是标题
2、这里是标题
3、这里是标题
————————————-
4、这里是标题
5、这里是标题
6、这里是标题

…..以此类推,这样的格式怎么取呢,我总以为要跳出循环,其实就下面的代码就可以了。如果有其他方法,希望能指点下。谢谢。

代码部分:

<?php
header("Content-Type:text/html; charset=utf-8");
	$result = array(
			'0' => array(
					'0'=>'1a',
					'2'=>'1a',
					'3'=>'1a',
					'4'=>'1a',

					),
			'1' => array(
					'0'=>'1b',
					'2'=>'1b',
					'3'=>'1b',
					'4'=>'1b',

					),

			'2' => array(
					'0'=>'1c',
					'2'=>'1c',
					'3'=>'1c',
					'4'=>'1c',

					),
			'3' => array(
					'0'=>'1d',
					'2'=>'1d',
					'3'=>'1d',
					'4'=>'1d',

					),

			'4' => array(
					'0'=>'1f',
					'2'=>'1f',
					'3'=>'1f',
					'4'=>'1f',

					),
			'5' => array(
					'0'=>'1e',
					'2'=>'1e',
					'3'=>'1e',
					'4'=>'1e',

					),
			'6' => array(
					'0'=>'1gg',
					'2'=>'1gg',
					'3'=>'1gg',
					'4'=>'1hhgg',

					),
			'7' => array(
					'0'=>'1hhgg',
					'2'=>'1ghhg',
					'3'=>'1hhgg',
					'4'=>'1ghhg',

					),
			'8' => array(
					'0'=>'1geeg',
					'2'=>'1eerrre',
					'3'=>'1rerer',
					'4'=>'1erer',

					),
			'9' => array(
					'0'=>'1ewrwe',
					'2'=>'1werwe',
					'3'=>'1werwer',
					'4'=>'1werwe',

					),
			);
			
$maxnums = count($result); //统计总的数组条数
?>
<div class="content-conveyor ui-helper-clearfix" style="width: 810px; left: 0px;">
<?php 
for($i=0;$i<$maxnums;$i=$i+1){ //从0开始循环
?>
<ul>
<?php 
	//从第一个数组开始循环 result[0]
	foreach ($result[$i] as $key=>$value){
?>   
   <li class="item"><?echo $value;?></li>
<?php 
	}
?>
</ul>
<?php 
}
?>
</div>



<?php 
echo "<hr>";
echo "另一种方法(Another)";
echo "<br>";

	foreach ($result as $key=>$value){
		?>
		<ul>
		<?php 
			foreach ($value as $k=>$v){
		?>
		<li class="item"><?echo $v;?></li>
		<?php 
			}
		?>
		</ul>
		<?php 
	}
?>

代码比较乱,初步测试可以得到以上效果。

还有一种是这样的,如下:

<?php
 header("Content-Type:text/html; charset=utf-8");
$result = array(
		0=>array(
	'uid'=>'1',
	'username'=>'标题0',
	),
			1=>array(
	'uid'=>'1',
	'username'=>'标题2',
	),
			2=>array(
	'uid'=>'1',
	'username'=>'标题2',
	),
			3=>array(
	'uid'=>'1',
	'username'=>'标题3',
	),
			4=>array(
	'uid'=>'1',
	'username'=>'标题4',
	),
			5=>array(
	'uid'=>'1',
	'username'=>'标题5',
	),
			6=>array(
	'uid'=>'1',
	'username'=>'标题6',
	),
			7=>array(
	'uid'=>'1',
	'username'=>'标1题7',
	),
				8=>array(
	'uid'=>'1',
	'username'=>'标1题7',
	),
);
 
$i=0;
$x = count($result);
foreach ($result as $key=>$value){
if($i%3==0) { //哨兵
echo "<ul>";
}
echo "<li>".$value['username']."</li>";
$i++;
if($i%3==0 && $i!= $x){  //哨兵
echo "</ul>";
}

if ($i == $x){ //最后一个的时候补上关闭标签
	echo "</ul>";
}
}
?>

3 Responses to “PHP一个数组以标签为单位循环”

  1. danielwang

    $clothes =array(
    0=>array(
    “休闲装”,
    “休闲装2″,
    ),
    1=>array(
    “休闲装”,
    “休闲装2″,
    ),
    2=>array(
    “西服1″,
    “西服2″,
    ),
    3=>array(
    “职业装”,
    “职业装2″,
    ),
    );

    array_walk ($clothes,’display’) ;
    function display($value, $key){
    if(is_array($value)){
    array_walk($value,’display’);
    }else{
    echo $value.”;
    }
    }
    ?>
    拙见!

    回复
  2. shadow

    < ?php
    $fruits = array("d" => “lemon”, “a” => “orange”, “b” => “banana”, “c” => “apple”);

    function test_alter(&$item1, $key, $prefix)
    {
    $item1 = “$prefix: $item1″;
    }

    function test_print($item2, $key)
    {
    echo “$key. $item2
    \n”;
    }

    echo “Before …:\n”;
    array_walk($fruits, ‘test_print’);

    array_walk($fruits, ‘test_alter’, ‘fruit’);
    echo “… and after:\n”;

    array_walk($fruits, ‘test_print’);
    ?>

    才翻出array_walk

    数组不全,帮你补上了,不知道对不对。

    效果
    Before …:
    d. lemon
    a. orange
    b. banana
    c. apple
    … and after:
    d. fruit: lemon
    a. fruit: orange
    b. fruit: banana
    c. fruit: apple

    回复

Leave a Reply

(will not be published)