3

I want to remove duplicate values from array. I know to use array_unique(array) function but faced problem in foreach loop. This is not a duplicate question because I have read several questions regarding this and most of them force to use array_unique(array) function but I have no idea to use it in foreach loop. Here is my php function.

$images = scandir($dir);
$listImages=array();
foreach($images as $image){
    $listImages=$image;
    echo substr($listImages, 0, -25) ."<br>"; //remove last 25 chracters
}

How to do this?

6
  • 4
    can you post ur expected output and input Commented Oct 14, 2016 at 12:37
  • 2
    please specify your full example Commented Oct 14, 2016 at 12:39
  • yes you can use array_unique after ending your foreach loop. Commented Oct 14, 2016 at 12:39
  • 1
    Use array_unique() ?? something like array_unique($images) Commented Oct 14, 2016 at 12:39
  • Your code is nothing to do with what you say. If you want to delete duplicates from an array in an alternative way please use a double iteration classic pattern Commented Oct 14, 2016 at 12:42

5 Answers 5

8

It is very complicated to remove duplicate values from array within foreach loop. Simply you can push all elements to one array and then remove the duplicates and then get values as you need. Try with following code.

   $listImages=array();
   $images = scandir($dir);

   foreach($images as $image){
       $editedImage = substr($image, 0, -25);
       array_push($listImages, $editedImage);
   } 

   $filteredList = array_unique($listImages);

   foreach($filteredList as $oneitem){
       echo $oneitem;
   }
Sign up to request clarification or add additional context in comments.

Comments

1

The example you provided could be modified as follows:

$images = scandir($dir);
$listImages=array();
foreach($images as $image) {
    if (!in_array($image, $listImages)) {
        $listImages[] = $image;
    }
    echo substr($image, 0, -25) ."<br>"; //remove last 25 chracters
}

Now $listImages will contain no duplicates, and it will echo every image (including duplicates).

3 Comments

@ mister martin, it will be optimized if will use array_unique($listImages) after loop end. for now you did check everytime in loop.
why to down vote... if answer is correct and different from you @mister martin
I want to print each and every value within the foreach loop. So cant use array_unique($listImages) after loop end
1

Based on @mistermartins answer:

$images = scandir($dir);
$listImages=array();
foreach($images as $image) {
    //if already echo'd continue to next iteration
    if (in_array($image, $listImages)) {
        continue;
    }
    //else, add image to array and echo.
    $listImages[] = $image;
    echo substr($image, 0, -25) ."<br>"; //remove last 25 chracters
}

Comments

0

It should be faster to use hashmaps:

$images = scandir($dir);
$listImages = array();
foreach($images as $image) {
    if (!isset($listImages[$image])) {
        $listImages[$image] = true;
        echo substr($image, 0, -25) ."<br>"; //remove last 25 chracters
    }
}

Comments

0

I'm not sure if I have understood you completely. Subsequent approach worked for me in order to remove duplicate indizes from array using a foreeach loop.

$list = array("hans", "peter", "hans", "lara", "peter", "lara", "lara");
sort($list);
foreach ($list as $k => $v) {
     if (isset($check)) {
        if ($check === $v) {
            unset($list[$k]);
        }
     }
     $check = $v;
}

$noDuplicate = array_values($list);

print_r($noDuplicate);

gives following result:

Array ( [0] => hans [1] => lara [2] => peter )

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.