从 PHP 中的 foreach 循环返回不同的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9377169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:41:18  来源:igfitidea点击:

Returning distinct values from foreach loop in PHP?

phploopsforeachdistinctecho

提问by hairynuggets

I have a foreach loop which echo's each of the property types in my search results. The code is as follows:

我有一个 foreach 循环,它echo是我搜索结果中的每个属性类型。代码如下:

<?php 
    foreach($search_results as $filter_result) {
        echo $filter_result['property_type'];
    } 
?>

The above code returns:

上面的代码返回:

house house house house flat flat flat

I would like to do something similar to the MySQL 'distinct', but I am not sure how to do it on a foreach statement.

我想做一些类似于 MySQL 'distinct' 的事情,但我不知道如何在 foreach 语句中做到这一点。

I want the above code to return:

我希望上面的代码返回:

  • house
  • flat
  • 房子
  • 平坦的

Not repeat every item each time. How can I do this?

不要每次都重复每个项目。我怎样才能做到这一点?

回答by hsz

Try with:

尝试:

$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'];
}

回答by ale

http://php.net/manual/en/function.array-unique.php

http://php.net/manual/en/function.array-unique.php

Example:

例子:

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input); 
print_r($result);

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

You will need to alter it slightly to check using the property_typepart of your array.

您需要稍微更改它以使用property_type数组的一部分进行检查。

回答by Widor

I'd use two loops here. One to build an array of distinct property_typefields (you can use code in the loop to check the item doesn't already exist).

我会在这里使用两个循环。一个构建不同property_type字段的数组(您可以在循环中使用代码来检查项目不存在)。

Then, use a second loop to step through the array and echothe list of items.

然后,使用第二个循环遍历数组和echo项目列表。

回答by inquam

You would have to keep track of already echoed values or build a new unique array of the values of all the $filter_result['property_type']. But that would then require you to iterate over that array once more to actually print. So keeping track would be better.

您必须跟踪已经回显的值或构建所有$filter_result['property_type']值的新唯一数组。但这将要求您再次迭代该数组以实际打印。所以跟踪会更好。

回答by nax

I was thinking there was some parameter to the in_array() function to get the count of the items found.

我在想 in_array() 函数有一些参数来获取找到的项目数。

But is doesn't exist.

但是是不存在的。

So try with array_unique().

所以尝试使用array_unique()

The better way is to duplicate the array before the foreachloop and apply this function.

更好的方法是在foreach循环之前复制数组并应用此函数。

回答by Eugen Rieck

<?php 

$filter=array();
foreach($search_results as $filter_result)
   $filter[]=$filter_result['property_type'];
$filter=array_unique($filter);

print_r($filter);
?>