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/>";
}
?>
There's a faster way, if you'd like to prematurely optimize.
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:Or this, for sorted:
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.
array_unique
should do it. Try this:You can add each unique element to a new array and then see if they exist with
in_array()
:You can try this -
Or
Instead of
Do
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:
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.
You should try with array store technique using array_unique