userknowmore
userknowmore

Reputation: 137

How to remove/hide duplicate/matching file name from array?

I am trying to remove or hide match file names from array and i also removed file extension but how to hide/remove duplicate file name if match?

If output 'demo1','demo1','demo2', then i want to do like this 'demo1','demo2',

Upvotes: 1

Views: 124

Answers (1)

Jashaszun
Jashaszun

Reputation: 9270

Use the array_unique function, which does exactly that: it removes duplicates.

<?php 
$images = array();
foreach (glob('{*.jpg,*.png}', GLOB_BRACE) as $key=>$image) {
    $images[] = substr($image, 0, -4);
}
$images = array_unique($images);
foreach ($images as $key=>$image) {
    if ($key == 0)
    { echo "'".substr($image, 0, -4)."'"; }
    else
    { echo ",'".substr($image, 0, -4)."'"; }
}
?>

Upvotes: 2

Related Questions