简体   繁体   English

仅从foreach返回唯一值

[英]Return only unique values from foreach

I'm currently trying to use a foreach to return all the addresses using the relation from my event model. 我目前正在尝试使用foreach来使用事件模型中的关系返回所有地址。 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. 我尝试了array_unique,但不确定语法是否正确。

<?php  
    foreach ($data->events as $address) {
        //array_unique($address, SORT_REGULAR);
        echo $address->getAddressString() ."<br/> <br/>";
    }
  ?>

You should try with array store technique using array_unique 您应该尝试使用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;;
}

You can add each unique element to a new array and then see if they exist with in_array() : 您可以将每个唯一元素添加到新数组,然后使用in_array()查看它们是否存在:

$uniques = [];
foreach($data->events as $address){
    if(!in_array($address->getAddressString(), $uniques)){
        $uniques[] = $address->getAddressString();
        echo $address->getAddressString()."<br><br>";
    }
}

There's a faster way, if you'd like to prematurely optimize. 如果您想过早地进行优化,则有一种更快的方法。

<?php  
    $arr = array();
    foreach ($data->events as $address) {
        $arr[$address->getAddressString()] = 'a'; // value doesn't matter
        // using inherent uniqueness of keys.
    }
    // $addrs = array_keys($arr);
    // optionally, take all those array keys and put them in values.
    // keys would become regular numeric keys
  ?>

That should run faster than any of the other answers here. 那应该比这里的其他任何答案都快。 But it will only make a difference if you are dealing with large amounts of data. 但这只会在您处理大量数据时有所作为。

If you want to echo, you will want to do array_keys if you didn't above. 如果您想回显,那么如果您不想要上面的内容,则需要执行array_keys Here it is in one line: 这是一行:

echo implode(', ',array_keys($arr)); // comma separated list of unique values

Or this, for sorted: 或对此,进行排序:

$addrs = array_keys($arr);
sort($addrs);
echo implode(', ',$addrs); // sorted list

Finally, I'd like to add that you should be getting unique, ordered results from your data model in the first place. 最后,我想补充一点,首先应该从数据模型中获得唯一的,有序的结果。 The database is much faster and better at simple tasks like ordering unique results than your PHP code ever will be. 在执行诸如订购独特结果这样的简单任务方面,该数据库要比PHP代码更快,更好。

SELECT DISTINCT `address` FROM `table`
WHERE `city` LIKE 'Lodi'
ORDER BY 'address' ASC

array_unique should do it. array_unique应该这样做。 Try this: 尝试这个:

<?php  
foreach (array_unique($data->events) as $address) {
    echo $address->getAddressString() ."<br/> <br/>";
}
?>

You can try this - 你可以试试这个-

<?php  
    $all_addresses= array();
    foreach ($data->events as $address) {
        $all_addresses[]= $address->getAddressString();
    }
    $all_addresses= array_unique($all_addresses);
    foreach($all_addresses as $val) {
       echo $val . "<br/> <br/>";
    }
?>

Or 要么

Instead of 代替

    foreach($all_addresses as $val) {
       echo $val . "<br/> <br/>";
    }

Do

    echo implode('<br/> <br/>', $all_addresses);

Even though the question was different I agree with Steve. 即使问题不同,我也同意史蒂夫的观点。 You should adjust your query. 您应该调整查询。 Its faster, more readable and maintainable. 它更快,更易读和可维护。 You don't want to do anything you don't need to do. 您不想做任何不需要做的事情。

If i gathered correctly you have to tables that are in relation. 如果我正确地收集了,您就必须关联表格。 Great. 大。 Try something like this: 尝试这样的事情:

$var1 = YourModel::model()
->with('address_table')
->findAllByAttributes(array(-optional if you want certain columns-), 
                     array('condition'=>'`type` LIKE :type AND `typeId` = :typeId AND `suggestionId` IS NULL', 'params'=>array(':type'=>$_GET['type'], ':typeId'=>$_GET['typeId']), 'group'=>'address_table.`column`'));

Most important thing here for you is the 'group' command which like the name says groups results and returns only unique ones. 对您而言,最重要的是'group'命令,该命令的名称表示分组结果,并且仅返回唯一的结果。 Be sure to prefix it correctly, either table name or alias depending on what you are working with. 请确保正确加上前缀(表名或别名),这取决于您使用的是什么。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM