How to remove an array of duplicate keys in a multidimensional array

I have this array, I want to delete the array if it is the same from the others

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

      

I want this result Monday and the time is displayed twice, so I only have to display once when I use below code to do this, so please provide a better solution for this

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

      

+3


source to share


4 answers


PHP 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); 

      



Output:

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
        )

)

      

+2


source


Standard O (n ^ 2) generic cloning solution:

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];
});

      

Example: http://sandbox.onlinephpfunctions.com/code/8013c36b78d24f08651b05375d686c9e07c75a49



If you want a more optimized version:

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

      

However, for the second version, you need to decide how to compare and order each item (which is not always possible).

+1


source


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

      

0


source


@Er Nilay Parekh you can do it like below:

<?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));

      

-1


source







All Articles