如何根据键从多维数组中删除重复值

时间:2015-05-26 05:03:44

标签: php arrays

简单问题有以下代码但不起作用

//Enter your code here, enjoy!
$correo[] = array('valor'=>'vikram.123@gmil.com','registro'=>'23');
$correo[] = array('valor'=>'vikram.123@gmil.com','registro'=>'24');
$correo[] = array('valor'=>'vikram.123@gmil.com','registro'=>'26');

function arrayUnique($myArray){
    if(!is_array($myArray))
        return $myArray;

    foreach ($myArray as &$myvalue){
        $myvalue=serialize($myvalue);
    }

    $myArray=array_unique($myArray);

    foreach ($myArray as &$myvalue){
        $myvalue=unserialize($myvalue);
    }

    return $myArray;

} 

$arr= arrayUnique($correo);

var_dump($arr);

我要做的是删除那些具有重复valor键的行,例如上面数组的结果应该是

Array ( 
    [0] => Array ( 
        [valor] => 'vikram.123@gmail.com' 
        [registro] => 24 
        ) 
)

3 个答案:

答案 0 :(得分:1)

你可以这样做......

$correo[] = array('valor'=>'vikram.123@gmil.com','registro'=>'23');
$correo[] = array('valor'=>'vikram.123@gmil.com','registro'=>'24');
$correo[] = array('valor'=>'vikram.123@gmil.com','registro'=>'26');
function arrayUnique($myArray, $uniqueKeyName)  
{ 
    // Unique Array for return  
    $myReturnArray = array();  
    // Array with the md5 hashes  
    $arrayHashes = array();  
    foreach($myArray as $key => $item) {        
        $hash = md5(serialize($item[$uniqueKeyName]));       
          if (!isset($arrayHashes[$hash])) {  
           $arrayHashes[$hash] = $hash;         
           $myReturnArray[] = $item;
        }  
    }  
    return $myReturnArray;  
} 
$arr= arrayUnique($correo, 'valor');
    echo "<pre>";
    print_r($arr);  
    echo "</pre>"; 

答案 1 :(得分:1)

您可以尝试这样做,因为您说过应该考虑registro中的任何一个。

 [akshay@localhost tmp]$ cat test.php
 <?php

 $array = array(
                 array('valor'=>'vikram.123@gmil.com','registro'=>'23'),
                 array('valor'=>'vikram.123@gmil.com','registro'=>'24'),
                 array('valor'=>'vikram.123@gmil.com','registro'=>'26')
               );

 function remove_dup($array, $key)
 {
    foreach( $array as $v )
    {
        if(! isset($out[ $v[$key] ]  ) )
            $out[$v[$key]] = $v;
    }
    return array_values($out);
 }


 // Input
 print_r( $array );

 // Output
 print_r( remove_dup($array,'valor') );

 ?>

<强> 输出

 [akshay@localhost tmp]$ php test.php
 Array
 (
     [0] => Array
         (
             [valor] => vikram.123@gmil.com
             [registro] => 23
         )

     [1] => Array
         (
             [valor] => vikram.123@gmil.com
             [registro] => 24
         )

     [2] => Array
         (
             [valor] => vikram.123@gmil.com
             [registro] => 26
         )

 )

 Array
 (
     [0] => Array
         (
             [valor] => vikram.123@gmil.com
             [registro] => 23
         )

 )

答案 2 :(得分:0)

您可以将功能定义为 -

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="badgeSignUp" value="{'component':'LP-Popup', 'flowType':'f', 'category': 'SM-Flight-HP', 'campaignId' : '5'}"></div>

function arrayUnique($myArray){ if(!is_array($myArray)) return $myArray; $temp = array(); foreach ($myArray as $myvalue){ $temp[$myvalue['valor']] = $myvalue; } return $temp; } 为键存储数组。它只存储最后一个。