0

I have an array and it has repeating items. So I trying to delete item if it repeats. For example:

$links:

  string(35) "/mjr/semba-tower/outline/index.html"
  [1]=>
  string(38) "/mjr/mc-futsukaichi/outline/index.html"
  [2]=>
  string(31) "/mjr/chihaya/outline/index.html"
  [3]=>
  string(35) "/mjr/semba-tower/outline/index.html"

As you see 2 semba-towers in the array and I want to delete one of if. I tried this, but output returns 0 item.

$output = [];
        foreach(array_count_values($links) as $value => $count)
        {
            if($count == 1)
            {
                $output[] = $value;
            }
        }

        var_dump($output); 

Any other way to fix this problem?

1 Answers1

1

Use the PHP array_unique() function

You can use the PHP array_unique() function to remove the duplicate elements or vlaues form an array. If the array contains the string keys, then this function will keep the first key encountered for every value, and ignore all the subsequent keys.

$links = array(
    "/mjr/semba-tower/outline/index.html",
    "/mjr/mc-futsukaichi/outline/index.html",
    "/mjr/chihaya/outline/index.html",
    "/mjr/semba-tower/outline/index.html"
);

// Deleting the duplicate items
$result = array_unique($links);
print_r($result);

OUTPUT:

Array ( [0] => /mjr/semba-tower/outline/index.html [1] => /mjr/mc-futsukaichi/outline/index.html [2] => /mjr/chihaya/outline/index.html )
Juan J
  • 413
  • 6
  • 12