July 22, 2013

Increment value in array php

Cipherous’s Question:

I am trying to make a function that grabs all the days that have events that are in a database for a certain user. For instance if there were two events on Jan 23, 2013 it would add Jan 23, 2013 to the array. I got it to work so it adds all the days (without adding the same day twice) but now I want to be able to say how many dates are on each day. So on Jan 23, 2013 it would say they have two events in that day.

I hope this makes sense… I have some code for further aid.

PHP Function (grabbing each day that has events)

//gets upcoming days that have events for a user
    public function get_upcoming_days_with_events() {
        $return_array = array();
        $date_in=null;
        $user_id = $this->session->userdata('id');
        $query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
        foreach ($query->result_array() as $event => $row) {
            $date = strtotime($row['date_due']);
            if (sizeof($return_array) != 0) {
                foreach ($return_array as $date_in_array => $row) {
                    $d = $row['full_date'];
                    if (date('Y-m-d', $date) == $d) {
                        //date is already in array
                        //increment the number of assignments on this day
                        $row['number_of_assignments'] += 1;
                        $date_in = true;
                    } else{
                        $date_in = false;
                    }
                }
            }
            if ($date_in == false) {
                $return_array[] = array(
                    'day' => date('d', $date),
                    'month' => date('m', $date),
                    'full_date' => date('Y-m-d', $date),
                    'number_of_assignments' => 1
                );
            }
        }
        return $return_array;
    }

So with this I want to be able to increment $return_array[‘number_of_assignments’] if my function notices that a certain day has more than one event.

Let me know if you need any more info…

Thanks! 🙂

We can save the info in return_array by index of date, if the date info have not been set into return_array, we make an empty info. Each time, we simply increase number_of_assignments.

 public function get_upcoming_days_with_events()
 {
     $return_array = array();
     $user_id = $this->session->userdata('id');
     $query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
     foreach ($query->result_array() as $event => $row)
     {
         $date = strtotime($row['date_due']);
         $date_key = date('Y-m-d', $date);
         if (!isset($return_array[$date_key]))
         {
             $new_item = array(
                 'day' => date('d', $date),
                 'month' => date('m', $date),
                 'full_date' => $date_key,
                 'number_of_assignments' => 0,
             );
             $return_array[$date_key] = $new_item;
         }
         $return_array[$date_key]['number_of_assignments']++;
     }
     $return_array = array_values($return_array);
     return $return_array;
 }

Function: array_count_values() can help you with this, it gives total number of occurance of a value in an array.

For example:

$a = array("apple", "banana", "apple");
var_dump(array_count_values($a)); 

Will output

array(
   [apple] => 2,
   [banana] => 1
);

So instead of trying to filter out duplicate events, add them all on the array and then use array_count_values() at last to know their occurenses.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!