从PHP中的foreach循环返回不同的值? [英] Returning distinct values from foreach loop in PHP?
本文介绍了从PHP中的foreach循环返回不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个foreach循环,该循环在搜索结果中每个echo
属性类型.代码如下:
I have a foreach loop which echo
's each of the property types in my search results. The code is as follows:
<?php
foreach($search_results as $filter_result) {
echo $filter_result['property_type'];
}
?>
上面的代码返回:
house house house house flat flat flat
我想做些类似于MySQL'distinct'的事情,但是我不确定如何在foreach语句上做到这一点.
I would like to do something similar to the MySQL 'distinct', but I am not sure how to do it on a foreach statement.
我希望上面的代码返回:
I want the above code to return:
- 房子
- 平
不每次都重复每个项目.我该怎么办?
Not repeat every item each time. How can I do this?
推荐答案
尝试使用:
$property_types = array();
foreach($search_results_unique as $filter_result){
if ( in_array($filter_result['property_type'], $property_types) ) {
continue;
}
$property_types[] = $filter_result['property_type'];
echo $filter_result['property_type'];
}
这篇关于从PHP中的foreach循环返回不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文