1

I have done a code to call an array of a value based on each date. My code is below:

$matchs = DiraChatLog::where('status','=','Match')->whereBetween('date_access', [$request->from, $request->to])->get();
$array[] = [];
foreach ($matchs as $key => $match) {
    $array[$match->date_access] = $match->status;
}

dd($array);

Using this I try and dd(); I get output like this: enter image description here

What I'm trying to do now is to first group the same dates together and also I want to then count the total for that dates. how can I do this?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Rajveer Singh
  • 433
  • 5
  • 20

2 Answers2

2

Not sure what you mean with "date". But if you mean the same day it would be this:

$matchs = DiraChatLog::where('status','=','Match')->whereBetween('date_access', [$request->from, $request->to])->get();
$array[] = [];
foreach ($matchs as $key => $match) {
 $day = substr($match->date_access, 0, 10);
 if(isset($array[$day])){
  $array[$day]++;
 }else{
  $array[$day] = 1;
 }
}

dd($array);
Bernhard
  • 1,852
  • 11
  • 19
  • this worked for me thanks! Also one more things, how can i declare this array to "match" that means i wanna call it match. so instead of dd(array); i wanna dd($match); ? – Rajveer Singh Mar 23 '18 at 07:27
  • if you want to do this you need to first rename all $match (variable already exists in your code!) to another name. then you rename all $array to $match. but this is really the most basic issue. i recommend you to read a view tutorials about php basics. – Bernhard Mar 23 '18 at 07:31
0
   $array= array();
   $arrayCount=array();
   foreach ($matchs as $key => $match) { 
       $time = strtotime($match->date_access); 
       $newformat = date('Y-m-d',$time);
       if(!array_key_exists($newformat , $array)){ 
                 $i=1;
            $array[$newformat]=$match->status; 
       }     

            //$array[$newformat]=$i;   remove this comment if you want the count inside
            $arrayCount[$newformat]=$i;
            $i++;
     }

in the event you want to maintain the "match" value in your array if not just un-comment the code and remover $arrayCount occurrences

Reuben Gomes
  • 878
  • 9
  • 16