如何删除多维数组中的重复键值数组

时间:2017-04-12 06:49:10

标签: php arrays sorting multidimensional-array

我有这个数组我想删除数组,如果它的值与其他数据相同

$appointments = array(
                      array('1','Monday', '5:30 PM'),
                      array('2','Friday', '6:00 PM'),
                      array('3','Monday', '5:30 PM'),
                      array('4','Tuesday', '4:15 PM'),
                      array('5','Wednesday', '8:30 PM'),
                      array('6','Thursday', '1:45 PM')
);

我想要这个结果星期一,时间显示两次,所以我只需要显示一次我在下面使用代码,所以请为此提供更好的解决方案

$one_dimension = array_map('serialize', $appointments);
$unique_one_dimension = array_unique($one_dimension);
$unique_multi_dimension = array_map('unserialize', $unique_one_dimension);

echo "<pre>";print_r($unique_multi_dimension);

4 个答案:

答案 0 :(得分:2)

PHP code demo

<?php

$appointments = array(
                      array('1','Monday', '5:30 PM'),
                      array('2','Friday', '6:00 PM'),
                      array('3','Monday', '5:30 PM'),
                      array('4','Tuesday', '4:15 PM'),
                      array('5','Wednesday', '8:30 PM'),
                      array('6','Thursday', '1:45 PM')
);
//Here we are finding column of week days.


$implodedColumns=  array_map(function($data){
    unset($data[0]);
    return implode(",", array_map('trim', $data));
}, $appointments);
$result=array_unique($implodedColumns); 

array_walk($result, function(&$value,$key){
    $value=  array_merge(array($key),  explode(",", $value));
});

print_r($result); 

<强>输出:

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => Monday
            [2] => 5:30 PM
        )

    [1] => Array
        (
            [0] => 1
            [1] => Friday
            [2] => 6:00 PM
        )

    [3] => Array
        (
            [0] => 3
            [1] => Tuesday
            [2] => 4:15 PM
        )

    [4] => Array
        (
            [0] => 4
            [1] => Wednesday
            [2] => 8:30 PM
        )

    [5] => Array
        (
            [0] => 5
            [1] => Thursday
            [2] => 1:45 PM
        )

)

答案 1 :(得分:1)

$one_dimension = array_map('serialize', $appointments);
$unique_one_dimension = array_unique($one_dimension);
$unique_multi_dimension[1] = array_map('unserialize', $unique_one_dimension);

echo "<pre>";print_r($unique_multi_dimension);die;

答案 2 :(得分:1)

标准O(n ^ 2)通用克隆解决方案:

function array_unique_callback($array, $equalityComparer) {
    $uniqueFiltered = [];
    $keys = array_keys($array);
    for ($i = 0;$i < count($array);$i++) {
         $add = true;
         for ($j = 0;$j < $i;$j++) {
              if ($equalityComparer($array[$keys[$i]], $array[$keys[$j]])) {
                   $add = false;
                   break;
              }
         } 
         if ($add) { 
             $uniqueFiltered[] = $array[$keys[$i]]; 
         }
    }
    return $uniqueFiltered;
}
array_unique_callback($appointments, function ($x,$y) {
     return $x[1] == $y[1] && $x[2] == $y[2];
});

实施例: http://sandbox.onlinephpfunctions.com/code/8013c36b78d24f08651b05375d686c9e07c75a49

如果您想要更优化的版本:

function array_unique_callback($array, $comparer) {
   //Comparer is not an equality comparer, it returns negative if $x less than $y, 0 if they are equal and 1 if $x greater than $y
    $keys = array_keys($array);
    uasort($array, $comparer);
    $uniqueFiltered = [];
    for ($i = 0;$i < count($array);$i++) {
        $uniqueFiltered[$keys[$i]] = $array[$keys[$i]]; 
        while ($i < count($array) && $comparer($array[$keys[$i]],$array[$keys[$i+1]]) === 0) { $i++; }
    }
    return $uniqueFiltered;
}

然而,第二个版本需要您决定比较和排序每个元素的方法(这并不总是可行)。

答案 3 :(得分:-1)

@Er Nilay Parekh你可以像下面的概念那样做:

<?php
  $appointments = array(
                      array('1','Monday', '5:30 PM'),
                      array('2','Friday', '6:00 PM'),
                      array('3','Monday', '5:30 PM'),
                      array('4','Tuesday', '4:15 PM'),
                      array('5','Wednesday', '8:30 PM'),
                      array('6','Thursday', '1:45 PM')
);
$isTimeExist = array();
foreach ($appointments as $key => $value) {
    if(in_array($value[2], $isTimeExist)){
        unset($appointments[$key]);
    }
    $isTimeExist[] = $value[2];
}
echo "<pre>";
print_r(array_values($appointments));