vendredi 5 octobre 2018

PHP - Count the amount of times the value in one array appears in another array, then create an array containing the numbers

I have two arrays, both in Y-m-d format. One array is the amount of days that has passed in the current month, and one array is a list of when every user on my app has been created in Y-m-d format. I want to create a third array which counts the amount of times a user was created on a certain day in my days-of-month array, then puts that count into the third array.

My code so far is this:

    $daysInCurrentMonth = date('t');
    $daysPast = date('j');
    $month = date('m');
    $year = date('y');

    $participants = DB::table('participants')->get();
    $createdAt = $participants->pluck('created_at');

    foreach($createdAt as $created)
    {
        $dateTimes[] = Carbon::createFromFormat('Y-m-d H:i:s', $created)->format('Y-m-d');
    }

    $dates = $this->generateDateRange(Carbon::parse($year.'-'.$month.'-01'), Carbon::parse($year.'-'.$month.'-'.$daysPast));

The $dates array has:

array (size=5)
  0 => string '2018-10-01' (length=10)
  1 => string '2018-10-02' (length=10)
  2 => string '2018-10-03' (length=10)
  3 => string '2018-10-04' (length=10)
  4 => string '2018-10-05' (length=10)

The $dateTimes array has:

array (size=6)
  0 => string '2018-09-21' (length=10)
  1 => string '2018-09-24' (length=10)
  2 => string '2018-09-24' (length=10)
  3 => string '2018-10-02' (length=10)
  4 => string '2018-10-04' (length=10)
  5 => string '2018-10-04' (length=10)

I want the third array to loop through all the days in $dates and for every date there is no tally put a 0, so given the above data my array would look like this:

$matches = [0, 1, 0, 2, 0]

I've tried a bunch of PHP array functions but I was wondering if someone could quickly help me out.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire