仅从 foreach 返回唯一值 [英] Return only unique values from foreach
本文介绍了仅从 foreach 返回唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试使用 foreach 使用我的事件模型中的关系返回所有地址.一切都很好,返回所有地址,但甚至会返回重复的结果.我尝试了 array_unique,但不确定我的语法是否正确.
I'm currently trying to use a foreach to return all the addresses using the relation from my event model. All is fine, returns all the addresses but will return even the duplicate results. I tried the array_unique but not sure I have the syntax correct.
<?php
foreach ($data->events as $address) {
//array_unique($address, SORT_REGULAR);
echo $address->getAddressString() ."<br/> <br/>";
}
?>
推荐答案
你应该试试 数组存储技术 使用 array_unique一个>
You should try with array store technique using array_unique
// First Store data in $arr
$arr = array();
foreach ($data->events as $address) {
$arr[] = $address->getAddressString();
}
$unique_data = array_unique($arr);
// now use foreach loop on unique data
foreach($unique_data as $val) {
echo $val;;
}
这篇关于仅从 foreach 返回唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文