Home > Backend Development > PHP Tutorial > PHP foreach循环如果查询出来的数据表里值相同则只循环一次怎么写

PHP foreach循环如果查询出来的数据表里值相同则只循环一次怎么写

WBOY
Release: 2016-06-23 13:15:37
Original
2270 people have browsed it

数据表字段分别是id name 字段下值有1 张三 2 李四 3 王五4 李四 我想实现foreach循环输出name的值  但是李四只输出一次


回复讨论(解决方案)

记录识别键,检查后续的键是否已输出过,是则跳过

记录识别键,检查后续的键是否已输出过,是则跳过



你好 这个需要怎么写  新手  能帮忙举个例子吗

foreach是输出一次,要用
$array = array();//定义一个数组,用于保存读取到的数据
$result=mysql_query(‘mysql语句');
while($row = mysql_fetch_array($result))
{
echo $row['name'];
}
试试这个吧!

<?php//首先从数据库中读出记录,格式如下$data = array(    array('name'=>'张三', 'age'=>16),    array('name'=>'李四', 'age'=>17),    array('name'=>'李四', 'age'=>17),    array('name'=>'王五', 'age'=>18),);echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';$name = array();foreach($data as $k=>$v){    if(!in_array($v['name'], $name)){ // 未使用过        $name[] = $v['name'];        echo $v['name'].' '.$v['age'].'<br>';    }}?>
Copy after login


张三 16
李四 17
王五 18

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template