3

I have this array I want to remove array if it value are same from 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 time is display two time so I have to display only one time I use below code for this so please provide 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);
5
  • so, which array are you going to remove/keep? the first ore the last? which values are important here - only second elements or first and thirds are also? Commented Apr 12, 2017 at 6:53
  • 5
    $appointments = array_intersect_key($appointments, array_unique(array_column($appointments, 1))); Commented Apr 12, 2017 at 6:55
  • any one but make sure duplicate will not display Commented Apr 12, 2017 at 6:55
  • I would use array key, go to the link: stackoverflow.com/questions/18734844/…. If you need help, just ask Commented Apr 12, 2017 at 7:07
  • Hello @MarkBaker its working only because u have use array_column but we have to compare both value week days and time also Commented Apr 12, 2017 at 7:22

4 Answers 4

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

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
        )

)
Sign up to request clarification or add additional context in comments.

6 Comments

hello but we dont have to compare only week days we have to compare weekdays and time also
@ErNilayParekh Sorry that was a mistake. You can check now
hello @sahil-gulati u provide sollution is working on this example but in my case there is tomany colums available so how can i compare?
@ErNilayParekh Can you provide a sample of that array.?
there are chat option available ?
|
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;

Comments

1

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 the second version needs you to decide on a way to compare and order each element (which is not always possible).

Comments

-1

@Er Nilay Parekh you can do it like below concept:

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

i don't like users who down vote to any one without any explanation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.