0

I have this Array :

Array (
[0] => Array ( [x] => 2016-04-19 ) 
[1] => Array ( [x] => 2016-05-25 ) 
[2] => Array ( [x] => 2016-05-26 ) 
[3] => Array ( [x] => 2016-05-27 ) 
[4] => Array ( [x] => 2016-05-28 ) 
[5] => Array ( [x] => 2016-05-29 ) 
[6] => Array ( [x] => 2016-05-29 ) 
[7] => Array ( [x] => 2016-06-02 ) 
[8] => Array ( [x] => 2016-06-03 ) 
[9] => Array ( [x] => 2016-06-07 ) 
[10] => Array ( [x] => 2016-06-10 ) 
[11] => Array ( [x] => 2016-06-17 ) 
[12] => Array ( [x] => 2016-06-24 ) )

I'm trying to count how many days are duplicates and get this number in a variable. For example as we can see there are 2 same dates:

2016-05-29

I've tried array_count_values() but it says it can only count strings and integers. That's correct because this is a Date variable. This is it's code:

$eventDates = $object->get("date");
$result = $eventDates->format("Y-m-d");
$data[] = array("x"=>$result);

Any idea of how to count them?

4
  • 1
    Array_unique? You first count and save to a variable, then array_unique and count again? Commented May 28, 2016 at 16:58
  • I would consider using array_map to create a string from each date value so that I could use array_count_values. Commented May 28, 2016 at 17:06
  • 1
    I think, its working good! eval.in/578631. The error for you is because, you counted the value of array (You are using multi-dimensional) Commented May 28, 2016 at 17:20
  • Did you give up or what??? Commented Jun 28, 2016 at 19:55

3 Answers 3

3

Using array_map you can convert the dates to strings, then you can use array_count_values:

$theArray = array(array('x' => new DateTime('2016-04-19')), 
    array('x' => new DateTime('2016-04-19')),
    array('x' => new DateTime('2016-04-19')),
    array('x' => new DateTime('2016-05-19')));

function formatDate($d) {
    return $d['x']->format('Y-m-d');
}
$results = array_map("formatDate", $theArray);

print_r(array_count_values($results));

.

Array (
    [2016-04-19] => 3
    [2016-05-19] => 1 
)

Then you can use this to determine the duplicates.

(This would also be useful if the dates contained time elements that you wanted to ignore.)

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

2 Comments

I really like your solution. There are some missing quotes in return $d['x']->format('Y-m-d');
Thank you, I've added the quotes. I'll post this for reference, [Why is $foo[bar] wrong?](fr2.php.net/manual/en/…).
2

Get the column you need and count the values:

$count = array_count_values(array_column($array, 'x'));

Then to find duplicates get the difference between the counts of 1 and the counts greater than 1:

$dupes = array_diff(array_flip($count), array_keys($count, 1));

1 Comment

The same I meant in comments :)
0

Did you say count only duplicates? Well, you may just loop through the Array of given Dates and bundle the Duplicates into a new array and afterwards get the count of the resulting array like so:

    <?php


        $arrHolder      = array();
        $arrDuplicates  = array();
        $arrDateList    = array(
            "date1"     => '2016-04-19',
            "date2"     => '2016-05-25',
            "date3"     => '2016-05-26',
            "date4"     => '2016-05-27',
            "date5"     => '2016-05-28',
            "date6"     => '2016-05-29',   //<== DUPLICATE DATE : 1
            "date7"     => '2016-05-29',   //<== DUPLICATE DATE : 2
            "date8"     => '2016-06-02',
            "date9"     => '2016-06-03',
            "date10"    => '2016-06-07',
            "date11"    => '2016-06-10',
            "date12"    => '2016-06-17',
            "date13"    => '2016-06-24',
            "date14"    => '2016-05-29',   //<== DUPLICATE DATE : 3
            "date15"    => '2016-05-29',   //<== DUPLICATE DATE : 4
        );

    // LOOP THROUGH ALL THE GIVEN ARRAYS CHECKING IF ANY OF THEM ALREADY EXIST
    // IF NOT, WE JUST PUSH THE DATE TO $arrHolder ARRAY
    // OTHERWISE WE PERFORM MORE CHECK AND PUSH IT INTO A MULTI-DIMENSIONAL ARRAY: $arrDuplicates.  
    foreach($arrDateList as $key=>$date){
        if(!in_array($date, $arrHolder)){
            $arrHolder[$key]                    = $date;
        }else{
            if(!array_key_exists($date, $arrDuplicates)){
                // IF THIS KEY EXIST, IT MEANS THAT THIS IS THE 2ND INSTANCE
                // SO WE ASSIGN A COUNT OF 2 TO IT.
                $arrDuplicates[$date]           = array($key=>$date, "count"=>2);
            }else{
                $arrDuplicates[$date]["count"]  =  intval($arrDuplicates[$date]["count"])  + 1;
            }
        }
    }

    var_dump($arrDuplicates);

RESULT OF VAR_DUMP

        // YOU CAN SEE THE COUNT (NUMBER OF DUPLICATES)
        // AS WELL AS THE KEY AND EVEN THE DATE.... :-) 
        // NO NEED FOR EXTRA LOGIC THE $arrDuplicates HAS THEM ALL
        // YOU ONLY HAVE TO PEEK IN....         
        array (size=1)
          '2016-05-29' => 
            array (size=2)
              'date7' => string '2016-05-29' (length=10)
              'count' => int 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.