Return only unique values from foreach

2019-07-28 11:20发布

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/>";
    }
  ?>

标签: php yii
6条回答
我只想做你的唯一
2楼-- · 2019-07-28 11:25

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. 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.

SELECT DISTINCT `address` FROM `table`
WHERE `city` LIKE 'Lodi'
ORDER BY 'address' ASC
查看更多
等我变得足够好
3楼-- · 2019-07-28 11:32

array_unique should do it. Try this:

<?php  
foreach (array_unique($data->events) as $address) {
    echo $address->getAddressString() ."<br/> <br/>";
}
?>
查看更多
倾城 Initia
4楼-- · 2019-07-28 11:34

You can add each unique element to a new array and then see if they exist with in_array():

$uniques = [];
foreach($data->events as $address){
    if(!in_array($address->getAddressString(), $uniques)){
        $uniques[] = $address->getAddressString();
        echo $address->getAddressString()."<br><br>";
    }
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-07-28 11:40

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);
查看更多
我只想做你的唯一
6楼-- · 2019-07-28 11:44

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. Be sure to prefix it correctly, either table name or alias depending on what you are working with. Hope it helps.

查看更多
时光不老,我们不散
7楼-- · 2019-07-28 11:48

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;;
}
查看更多
登录 后发表回答