如何在foreach循环中从数组中删除重复的值?

时间:2021-11-13 07:58:05

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.

我想从数组中删除重复的值。我知道使用array_unique(数组)函数,但在foreach循环中遇到了问题。这不是一个重复的问题,因为我已经读过几个关于这个问题的问题,其中大多数问题都要求使用array_unique(array)函数,但我不知道如何在foreach循环中使用它。这是我的php函数。

$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?

如何做到这一点呢?

5 个解决方案

#1


4  

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.

在foreach循环中从数组中删除重复的值是非常复杂的。只需将所有元素推到一个数组中,然后删除重复的元素,然后根据需要获取值。尝试使用以下代码。

   $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;
   }

#2


0  

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).

现在$listImages将不包含重复的内容,并且它将回显每个映像(包括重复的内容)。

#3


0  

Based on @mistermartins answer:

基于@mistermartins回答:

$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
}

#4


0  

It should be faster to use hashmaps:

使用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
    }
}

#5


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.

我不确定我是否完全理解你。后续的方法为我工作,以便从数组中删除重复的indizes。

$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 )

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

#1


4  

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.

在foreach循环中从数组中删除重复的值是非常复杂的。只需将所有元素推到一个数组中,然后删除重复的元素,然后根据需要获取值。尝试使用以下代码。

   $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;
   }

#2


0  

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).

现在$listImages将不包含重复的内容,并且它将回显每个映像(包括重复的内容)。

#3


0  

Based on @mistermartins answer:

基于@mistermartins回答:

$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
}

#4


0  

It should be faster to use hashmaps:

使用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
    }
}

#5


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.

我不确定我是否完全理解你。后续的方法为我工作,以便从数组中删除重复的indizes。

$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 )

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