0

I have a code which gives me list of addresses in my view file but some addresses are repeated. I don't want to display the ones which are already displayed.

$address_array[] = '-- Select address --';

foreach($address as $addres){
    $address_array[$addres->id] = $addres->town;
}

Any idea on stopping the repetition?

2 Answers 2

3

With the in_array function, you can check if a value already exists in your array

$address_array[] = '-- Select address --';

foreach($address as $addres){
    if (!in_array($addres->town, $address_array)) $address_array[$addres->id] = $addres->town;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get answer in 3 mins but stackoverflow won't allow me to accept. That script did the magic. Thank you
Sorry, I'll be slower next time ;)
3

Use array_unique to erase duplicates.

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

A little example :

$address = array_unique($address);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.