Removing duplicate values from an array

Asked

Viewed 31 times

2

I am managing to do the listing in the correct way, but some values are duplicated and I wanted to remove them and I can not at all... if anyone can help me, I am very grateful.

// Creating variable

$sector_list = $s->getSector();

// Listing

<?php foreach($sector_list as $value): ?>
    <option>
        <?php echo $value['sector']; ?>
    </option>   
<?php endforeach; ?>

The Return happens correctly, but with duplicate values.

  • array_unique() https://www.w3schools.com/php/func_array_unique.asp

1 answer

1

According to the php documentation, you can use the function array_unique.

The first parameter is the array, and the second is SORT_FLAG (not required).. Below is an example:

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);

// Irá printar:
/// Array
// (
//     [a] => green
//     [0] => red
//     [1] => blue
// )

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.